2016-09-30 10 views
1

Androidで双方向データバインディングを使用しようとしていますが、次のようなエラーが発生します。あなたが見るこのタイトル。com.example.widget.MyTableRowの値型java.lang.Stringで属性 'app:rowContentText'のゲッターが見つかりません

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})、双方向ビンの場合は失敗(属性のゲッターが見つかりません)鼎。

また、この問題について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のバグですか?

+0

のようなものでなければなりませんが、ゲッターを持つクラスはMyShopTableRow''です。レイアウトファイルでMyShopTableRowを使用していることを確認してください。 –

+0

@GeorgeMount、あいまいな記述と間違ったコードを残念に思っています(それは、いくつかの重要な情報を隠しているからですが、変更は絶対に正しくありません)。 – 2BAB

答えて

2

イベントrowContentTextAttrChangedを定義しておらず、イベントがトリガーしたときに、新しい値を取得するメソッドを使用します。

それはエラーが `MyTableRow`に言及

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あなたの答えに触発されて、私はその記事をもう一度読んで、最後に問題を解決しました。ありがとうございました。 – 2BAB

関連する問題