Search code examples
androidandroid-databindingnumberpicker

how to pass NumberPicker value (onValueChange) through Data Binding?


Android Number picker has: onValueChange(NumberPicker picker, int oldVal, int newVal)

how to pass those parameters (NumberPicker picker, int oldVal, int newVal) to my viewmodel through databind?

<data>
    <import type="android.view.View" />
    <variable name="vm" type="com.my.view.model.MainActivityVM" />
</data>
/* ... bla ...*/
<NumberPicker
    android:onValueChange="@{() -> vm.callMethod(/*???whats here???*/)}" />

I want to catch the new value in my view model everytime it changed.


Solution

  • Like this:

        android:onValueChange="@{(picker,oldv,newv) -> viewModel.onValChange(oldv, newv)}">
    

    I chose to have the signature of my ViewModel class take only the old and new values, not the NumberPicker. That's up to you.