(French here, sorry for potential misunderstandings)
I am new to databinding on Android and I am struggling with a rather simple issue.
Let's say I have this POJO which we will consider as part of my Model
public class User
{
public String name;
public User(String name)
{
this.name = name;
}
}
Now I have a ViewModel that contains a User
public class MyViewModel extends ViewModel
{
private User user = new User("blue");
public String getName()
{
return user.name;
}
public void setName(String name)
{
user.name = name;
}
}
I want the View (Activity) to be able to make a two-way databinding on this "name" field. I know how to do the Activity and XML stuff like seting up the Binding class etc. What I don't know is, how to make the ViewModel observable for any change on the "name" field of the User class. Note that I don't want to make my User class observable by doing :
public class User
{
public MutableLiveData<String> name;
public User(String name)
{
this.name.setValue(name);
}
}
because I personally prefer keeping this Android stuff away from my Model.
How can I change my ViewModel so that the View can listen to changes of "name" ? I've seen some things with the @Bindable annotation but I'm not quite sure on how to use it.
Thanks for your help