2016-09-30 37 views
1

我試圖在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嗎?

+0

的錯誤中提到'MyTableRow',但有吸氣劑類是'MyShopTableRow'。確保在佈局文件中使用MyShopTableRow。 –

+0

@GeorgeMount,對不起,我的模糊描述和錯誤的代碼(這是因爲我隱藏了一些敏感的信息,但修改不是絕對正確的。) – 2BAB

回答

2

您沒有定義事件rowContentTextAttrChanged,當事件觸發時,使用哪種方法獲取新值。

它應該是這樣的

InverseBindingMethods({InverseBindingMethod(
     type = android.widget.TextView.class, 
     attribute = "android:text", 
     event = "android:textAttrChanged", 
     method = "getText")}) 
    public class MyTextViewBindingAdapters { ... } 
+0

https://halfthought.wordpress.com/2016/03/23/2-way -data-binding-on-android /,受到你答案的啓發,我再次閱讀那篇文章,並最終解決了問題,請遵循文章。謝謝。 – 2BAB

相關問題