【发布时间】:2016-09-30 02:02:03
【问题描述】:
我正在尝试在 Android 中使用双向数据绑定,但它会引发类似标题的错误。
MyTableRow 是一个自定义视图组:
public class MyShopTableRow extends RelativeLayout {
...
public void setRowContentText(String text) {
mRowInputEt.setText(text);
}
public String getRowContentText() {
if(TextUtils.isEmpty(mRowInputEt.getText())){
return "";
}
return mRowInputEt.getText().toString();
}
}
然后我在 XML 文件中使用它:
<data>
<variable
name="shopInfo"
type="com.example.TableModel" />
</data>
<com.example.widget.MyTableRow
android:layout_width="match_parent"
android:layout_height="46dp"
android:layout_marginTop="11dp"
...
app:rowInputType="text"
app:size="regular"
...
app:rowContentText="@={shopInfo.shopName}"/>
模型文件是:
public class TableModel {
public String shopName = "lalala";
....
}
片段仅在单向数据绑定 (@{shopInfo.shopName}) 时有效,但如果是双向绑定则失败(找不到属性的 getter)。
我还找到了关于这个问题的question,但答案对我不起作用。
//The answer will throw an error below
//Error:(55, 17) Could not find event 'rowContentTextAttrChanged' on View type 'com.example.MyTableRow'
@InverseBindingMethods({
@InverseBindingMethod(type = MyShopTableRow.class, attribute = "rowContentText"),
})
public class MyShopTableRow extends RelativeLayout {
...
}
是IDE还是dataBinding库的bug?
【问题讨论】:
-
错误提到了
MyTableRow,但是有getter的类在MyShopTableRow上。确保在布局文件中使用 MyShopTableRow。 -
@GeorgeMount,很抱歉我的模棱两可的描述和错误的代码(那是因为我隐藏了一些敏感信息,但修改不是绝对正确的。)
标签: android data-binding