我試圖在Android中使用雙向數據綁定,但它會拋出類似如下的錯誤你看到這個標題。在com.example.widget.MyTableRow上找不到屬性「app:rowContentText」的值類型爲java.lang.String的getter
MyTableRow是一個自定義的ViewGroup:
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),如果它是雙向bin丁。
我也發現了一個關於這個問題的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 lib的bug嗎?
的錯誤中提到'MyTableRow',但有吸氣劑類是'MyShopTableRow'。確保在佈局文件中使用MyShopTableRow。 –
@GeorgeMount,對不起,我的模糊描述和錯誤的代碼(這是因爲我隱藏了一些敏感的信息,但修改不是絕對正確的。) – 2BAB