3

I've got a button:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:binding="http://schemas.android.com/tools">

    <data>

        <variable
            name="vm"
            type="com.MyViewModel" />
    </data>
    <Button
                binding:singleParameterString="test"
                binding:firstStringParameter="@{vm.userName}"
    .../>
...

The BindingAdapter looks as follows:

@BindingAdapter("binding:singleParameterString", "binding:firstStringParameter")
        fun setFormattedString(btn: Button, singleParameterString: String, firstStringParameter: String) {
            btn.text = String.format(singleParameterString, firstStringParameter)
        }

This doesn't work, it gives the following error: "Cannot find the setter for attribute 'binding:firstStringParameter' with parameter type java.lang.String on android.widget.Button.". However, if I change binding:firstStringParameter="@{vm.userName}" to, for example, binding:firstStringParameter="my lovely string" this will work.

The userName in the ViewModel is val userName: String = "my name" and if I try android:text="@{vm.userName}" that works. So the problem is in the databinding. I've got another project where I used the same code and it works... Sounds like a bug in the databinding/adaperbinding framework, but maybe someone came across this problem?

P.S. I also tried binding:firstStringParameter="@{ + vm.userName}", did not work.

By looking at the autogenerated Binding class I discovered a very strange code:

blah.setFormattedString(this.mboundView1, (java.lang.String)null, javaLangStringVmUserName);
4

2 回答 2

7

好的,图书馆坏了。我找到了解决方案。仅当两个参数都是硬编码字符串或从 ViewModel 传递时,它才会起作用。

以便

binding:firstStringParameter="@{vm.
binding:singleParameterString="@{vm.test}"

这有效:

 binding:firstStringParameter="userName"
 binding:singleParameterString="test"

,但这不起作用:

 binding:firstStringParameter="@{vm.userName}"
 binding:singleParameterString="test"
于 2018-09-11T14:46:16.860 回答
2

我找到了解决此问题的方法:

binding:firstStringParameter="@{notification.user.pic}"
binding:singleParameterString="@{`test`}"
于 2019-12-30T14:47:07.067 回答