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?
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}"/>
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}
.
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.
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