gpt4 book ai didi

Android : Databinding, notifyPropertyChanged() 不工作?

转载 作者:可可西里 更新时间:2023-11-01 19:06:36 25 4
gpt4 key购买 nike

我正在使用 Android 的数据绑定(bind)库。我的数据对象扩展了 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);
}
}

我的 xml 也被绑定(bind)了

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

绑定(bind)发生在初始值上。但是当我使用

更改值时
object.setTotalText(someSpannableString);

更改不会反射(reflect)在 TextView 中。可能是什么问题?

最佳答案

使用字段名而不是 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}"/>

关于Android : Databinding, notifyPropertyChanged() 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34294916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com