0

I am implementing two way data binding on custom View. I followed the official android developers but still can't make it work. I have a knob that controlls integer value inside the value property.

class ControlKnob(context: Context, attributeSet : android.util.AttributeSet) : RelativeLayout(context, attributeSet), IUIControl {
    companion object {
        @JvmStatic
        @BindingAdapter("value")
        fun setValue(knob : ControlKnob, value : Int) {
            if(knob.value != value) {
                knob.value = value
            }

        }

        @JvmStatic
        @InverseBindingAdapter(attribute = "value")
        fun getValue(knob : ControlKnob) : Int {
            return knob.value
        }

        @JvmStatic
        @BindingAdapter("app:valueAttrChanged")
        fun setListeners( knob : ControlKnob, attrChange : InverseBindingListener) {
            knob.setOnProgressChangedListener {
                attrChange.onChange()
            }
        }
    }
    var value : Int = -1
    set(value) {
        field = value
        valueView.text = stringConverter.invoke(value)
    }
....
....
}

Inside layout i use it like this:

<cz.abc.def.package.controls.ControlKnob
                    android:id="@+id/knob"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_row="0"
                    android:layout_column="0"
                    app:value="@={viewModel.value}"
                    app:label="Knob" />

And my view model:

@Bindable
fun getValue() : Int {
    return someValue
}

fun setValue(value : Int) {
    someValue = value

}

But still i can't compile it. I get

Cannot find a getter for cz.abc.def.package.controls.ControlKnob app:value that accepts parameter type 'int'

If a binding adapter provides the getter, check that the adapter is annotated correctly and that the parameter type matches.

What could be the cause of this ?

0

2 Answers 2

1

I figured it out. It turned out that it is not problem with the code. I was missing the apply plugin: 'kotlin-kapt' in the gradle build file. After i added this line into build.gradle in the module it worked.

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe your listener binding adapter is wrong? Per the documentation, the event listener BindingAdapter value should be "android:valueAttrChanged" and you have "app:valueAttrChanged".

1 Comment

changing the listener binding adapter to android:valueAttrChanged didn't help. Still got the same error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.