Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Databinding, notifyPropertyChanged() not working?

I am using Android's data binding library. I have my data object extending BaseObservable.

public static class SimpleData extends BaseObservable implements Serializable {
    private String text, subText;
    private SpannableString totalText;

    @Bindable
    public SpannableString getTotalText() {
      return totalText;
    }

    public void setTotalText(SpannableString totalText) {
      this.totalText = totalText;
      notifyPropertyChanged(BR.totalText);
    }
}

And my xml is binded as well

<TextView
    android:id="@+id/patient_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_marginLeft="16dp"
    android:layout_toRightOf="@+id/patient_image"
    android:textColor="@color/primary_text"
    android:text="@{object.getTotalText()}"/>

The binding takes place for the initial values. But when I change the value using

object.setTotalText(someSpannableString);

the changes are not reflected in the text view. What could be the problem?

like image 731
Ashwin Avatar asked Dec 15 '15 16:12

Ashwin


Video Answer


3 Answers

Using field's name instead of getter.

<TextView
    android:id="@+id/patient_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_marginLeft="16dp"
    android:layout_toRightOf="@+id/patient_image"
    android:textColor="@color/primary_text"
    android:text="@{object.totalText}"/>
like image 61
LenaYan Avatar answered Oct 11 '22 14:10

LenaYan


Need to add = for two way data binding:

android:text="@={LoginViewModel.address}"

I forgot to add = so its didn't work. If you are using EditText then want two way data binding by base BaseObservable then you need @={LoginViewModel.address} instead of @{LoginViewModel.address}.

like image 33
krishan mohan verma Avatar answered Oct 11 '22 15:10

krishan mohan verma


I just had the same problem. My binding would work the first time and then would not work the second time.

I had an identical setup to yours except I had put @Bindable on my setter and not my getter method.

Adding @Bindable to both my setter and getter fixed this issue for me.

When I debugged the inner workers of the data binding library I noticed that a method called request re-bind was not being called because it did an operation to check if the field was updated or not from the last value. My guess is you need the annotation on both methods so that it can do this internal confirmation to check if it needs to re-bind or not.

I'm not 100% if this is true or not, but having the annotation on both methods fixed my issue. Looking at the Data Binding library documentation I notice they just show the annotation on the getter.

You can try:

@Bindable
public SpannableString getTotalText() {
  return totalText;
}

@Bindable
public void setTotalText(SpannableString totalText) {
  this.totalText = totalText;
  notifyPropertyChanged(BR.totalText);
}

See if it resolves it.

like image 2
Dave Thomas Avatar answered Oct 11 '22 16:10

Dave Thomas