Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Databinding: ObservableField of custom object it is not working properly

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();
    }
}
like image 446
Rahul Chaurasia Avatar asked Jan 28 '23 17:01

Rahul Chaurasia


1 Answers

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.

like image 120
enyciaa Avatar answered Jan 31 '23 19:01

enyciaa