我正在尝试在 Android 中使用双向数据绑定(bind),但它会抛出一个错误,就像您看到的这个标题一样。
MyTableRow 是一个自定义 View 组:
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";
....
}
当它只是一种单向数据绑定(bind) (@{shopInfo.shopName}) 时,Snippet 可以工作,但如果它是双向绑定(bind),则会失败(无法找到属性的 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 lib的bug?
最佳答案
您没有定义事件 rowContentTextAttrChanged 以及当事件触发时,使用哪个方法获取新值。
应该是这样的
InverseBindingMethods({InverseBindingMethod(
type = android.widget.TextView.class,
attribute = "android:text",
event = "android:textAttrChanged",
method = "getText")})
public class MyTextViewBindingAdapters { ... }
关于android - 在 com.example.widget.MyTableRow 上找不到值类型为 java.lang.String 的属性 'app:rowContentText' 的 getter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39782557/