Cannot find the GETTER for attribute 'app:vm' with value type Boolean
Asked Answered
V

2

6

I'm trying to use native 2-way android data binding in my custom control

so I have something like that in xml

<layout>
<data>
<variable name="item" type="Boolean"/>
</data>
...
<my.control app:vm="@={item}"/>
...
</layout>

Please note, it's question about @={} - native 2 way binding.


and something like that in code:

class MyControl extends RelativeLayout{
...
@BindingAdapter("app:vm")
public static void setVm(View v, VM vm){...}
}

My questions - how should I define getter for my viewModel? I don't find any guidance about it. I tryed different approaches - write custom getter, static getters but error still the same.

Venesection answered 27/6, 2016 at 13:25 Comment(0)
E
7

Taken from here, under "Rolling Your Own":

You'll need a little more extra code to get the two-way databinding working with custom classes. Most importantly, you'll need to define a @InverseBindingMethod:

@InverseBindingMethods({
   @InverseBindingMethod(type = MyControl.class, attribute = "vm"),
})

In this case, the name of the getter matches the name of the attribute “getVm” for “app:vm.” (Changed to your example)

Please visit the linked blog- it has more information on that topic, including the binding of a attribute changed event listener.

Emplane answered 27/6, 2016 at 13:53 Comment(1)
Thanks, that link helped me with 2 way binding for RadioGroup, I found out there is a premade binding for it : android:checkedButtonEmlynn
H
0

Kotlin Version:

object DataBindingUtil {
    @BindingAdapter("emptyIfZeroText")    //use this instead "android:text"
    @JvmStatic
    fun setText(editText: EditText, text: String?) {
        if (text == "0" || text == "0.0") editText.setText("") else editText.setText(text)
    }

    @InverseBindingAdapter(attribute = "emptyIfZeroText", event = "android:textAttrChanged")
    @JvmStatic
    fun getText(editText: EditText) = editText.text.toString()
}
Huckster answered 30/10, 2020 at 21:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.