Android : Databinding, notifyPropertyChanged() not working?

匿名 (未验证) 提交于 2019-12-03 01:38:01

问题:

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?

回答1:

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


回答2:

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.



回答3:

I think you should define your String values as public not a private. Also dataBinding detects getter and setter automatically, so just type "@{object.totalText}" as other already said.

I hope this Youtube Link will be helpful to you too.

https://www.youtube.com/watch?v=h6ejIx5xu-M



回答4:

Use "notifyChange()" instead of "notifyPropertyChanged(BR._)" ..!



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!