How do I trigger a button click in my layout file?...
# android-databinding
v
How do I trigger a button click in my layout file? I have this onClick on my Button xml. But I can’t create an instance on my person class because it keeps saying
Cannot find Identifier Person
. How do I resolve this?
Copy code
android:onClick="@{() -> viewModel.doSomething(Person())}"
m
I don't think you can construct new objects in a data binding expression. Have
doSomething()
create the
Person
object. Or, create
doSomethingWithANewPerson()
that you call from the data binding expression, where it creates the
Person
object and calls
doSomething()
.
👍 1
r
You are probably missing an import. However, normally you would bind a person instance as variable into the layout so you can call
android:onClick="@{() -> viewModel.doSomething(person)}"
👍 1
r
Copy code
Person()
You defenetly can’t construct new objects using
new
operator. Use @Mark Murphy suggestion. Or if you really need to create a new object instance using databinding - use static methods like
Person.newInstance
k
When you bind the data in your Activity, Fragment or ViewHolder class you should add a variable for that person; then you declare that object as an extra variable alongside your viewmodel in your xml.