Android:数据绑定,notifyPropertyChanged()不起作用? [英] Android : Databinding, notifyPropertyChanged() not working?

查看:806
本文介绍了Android:数据绑定,notifyPropertyChanged()不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android的数据绑定库.我的数据对象扩展了BaseObservable.

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);
    }
}

我的xml也被绑定了

<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?

推荐答案

使用字段名称而不是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:数据绑定,notifyPropertyChanged()不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆