我有一个模型类
public class ScopeDefination extends BaseObservable {
String value;
String key;
boolean isPrivate;
String category;
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public ScopeDefination(String key, String value, boolean isPrivate) {
this.key = key;
this.value = value;
this.isPrivate = isPrivate;
}
public ScopeDefination(String key, String value, boolean isPrivate, String category) {
this.key = key;
this.value = value;
this.isPrivate = isPrivate;
this.category = category;
}
@Bindable
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;notifyPropertyChanged(developer.manish.publicprivatescopecategory.BR.key);
}
@Bindable
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
notifyPropertyChanged(developer.manish.publicprivatescopecategory.BR.value);
}
public boolean isPrivate() {
return isPrivate;
}
public void setPrivate(boolean aPrivate) {
isPrivate = aPrivate;
}
}
我试图在类中使用它来进行数据绑定,就像这样
ScopeDefination scopeDefination1 = new ScopeDefination(Keys.ADDRESS_HOME,"Address", true, Keys.FINANCIAL_INFO);
ScopeDefination scopeDefination2 = new ScopeDefination(Keys.NAME,"Name", true, Keys.FINANCIAL_INFO);
ObservableArrayMap<String, Object> user = new ObservableArrayMap<>();
user.put(Keys.ADDRESS_HOME, scopeDefination1);
user.put(Keys.NAME, scopeDefination2);
fragmentFirebaseBinderBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_firebase_binder, container, false);
fragmentFirebaseBinderBinding.setUser(user);
我的XML是
<?xml version="1.0" encoding="utf-8"?>
<data>
<import type="developer.manish.publicprivatescopecategory.ScopeDefination" />
<import type="android.databinding.ObservableMap" />
<variable
name="user"
type="ObservableMap<String, Object>" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="developer.manish.publicprivatescopecategory.FirebaseBinder">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text='@{((ScopeDefination)user["username"]).getValue()}'/>
<TextView
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text='@{((ScopeDefination)user["address_home"]).getValue()}' />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text='@{((ScopeDefination)user["username"]).getValue()}' />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text='@{((ScopeDefination)user["address_home"]).getValue()}' />
</LinearLayout>
我能够在用户界面上显示数据,但是当我在Editext中编辑数据时,相应的文本视图未获得更新。
答案 0 :(得分:0)
你想要的是双向绑定,它与普通绑定略有不同。请参阅George Mount的blog entry。
基本上,您需要从
编辑Binding表达式android:text='@{((ScopeDefination)user["username"]).getValue()}'
到
android:text='@={((ScopeDefination)user["username"]).value}'
请注意=
和@
之间有一个额外的{
。只要EditText
中的数据发生变化,这将通知您的模型。此外,您无法使用Two-Way Binding
中的方法,但使用.value
将起作用。感谢@George Mount指出这一点。