Reputation: 1312
This is seemingly old at this point. I have been reviewing Android Data Binding Documentation
As well pouring over posts here on SO however nothing seems to work.
No matter how I format the XML I get the same results. When I originally got this working (using lambda without passing arguments) took me a lot of trial and error. Now that i need to pass View in an onClick, im back to trial and error yet nothing is functional.
MainViewModel.java
private void navClicked(@NonNull View view) {
switch (view.getId()) {
case R.id.btn1:
break;
case R.id.btn2:
break;
}
}
public void testBtn() {}
activity_main.xml
<data>
<variable
name="mainViewModel"
type="com.example.viewmodel.MainViewModel" />
</data>
<!-- Works perfectly -->
<!-- however I would need a method for every button and that becomes redundant -->
<Button
android:id="@+id/testBtn"
android:onClick="@{() -> mainViewModel.testBtn()}"
android:text="@string/testBtn" />
<!-- "msg":"cannot find method navClicked(android.view.View) in class com.example.viewmodel.MainViewModel" -->
<Button
android:id="@+id/btn1"
android:onClick="@{(v) -> mainViewModel.navClicked(v)}"
android:text="@string/btn1" />
<!-- "msg":"cannot find method navClicked(android.view.View) in class com.example.viewmodel.MainViewModel" -->
<Button
android:id="@+id/btn2"
android:onClick="@{mainViewModel::navClicked}"
android:text="@string/btn2" />
<!-- "msg":"Could not find identifier \u0027v\u0027\n\nCheck that the identifier is spelled correctly, and that no \u003cimport\u003e or \u003cvariable\u003e tags are missing." -->
<!-- This is missing (view) in the lambda - makes sense to fail -->
<Button
android:id="@+id/btn3"
android:onClick="@{() -> mainViewModel.navClicked(v)}"
android:text="@string/btn2" />
Upvotes: 1
Views: 1953
Reputation: 1
Once I tried this and it worked:
XML:
android:onClick="@{() -> model.clickEvent(123)}"
Model:
public void clickEvent(int id) {...}
I used string for id but I think it also works with integer.
Upvotes: 0
Reputation: 1312
navClicked() was private...
// **denotes change, will not compile like that**
**public** void navClicked(@NonNull View view) {
switch (view.getId()) {
case R.id.btn1:
break;
case R.id.btn2:
break;
}
}
Upvotes: 1