In a test app I've one activity with one Edittext and a Button. Edittext is pointing to ObservableField of Person's name and on click of button, I'm modifying the person's name so that text inside the edittext should get changed. But changing the person name is not updating inside the edittext.
When it is working: It is working if I create a new Person object and set it to the observable field.
When it is not working But if I modify the same person object, does anybody know why it does not work in this case?
Person.java:
public class Person {
private int age;
private String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
PersonViewModel:
public class PersonViewModel {
public final ObservableField<Person> studentObservableField = new ObservableField<>();
public PersonViewModel(Person person) {
studentObservableField.set(person);
}
public void onButtonClick() {
// NOT WORKING
// Person person = studentObservableField.get();
// person.setName("Mohit");
// studentObservableField.set(person);
// WORKING FINE IN THIS CASE
Person person = new Person(21, "Mohit");
studentObservableField.set(person);
}
}
layout_file.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewmodel"
type="com.rahulchaurasia.databindingtest.PersonViewModel"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.rahulchaurasia.databindingtest.MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="@{viewmodel.studentObservableField.name}"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click me"
android:onClick="@{()-> viewmodel.onButtonClick()}"/>
</LinearLayout>
</layout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
activityMainBinding.setViewmodel(new PersonViewModel(new Person(21, "Rohit")));
activityMainBinding.executePendingBindings();
}
}
After you update a field inside the Person object, make sure you call notifyChange() on the ObservableField
studentObservableField.notifyChange()
This notifies everything which is listening to Person that an update to Person has occurred, and will rebind the values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With