1

I'm trying to use two-way data binding in Android, but it throws an error like this title you see.

MyTableRow is a custom 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();
    }
}

Then I use it in XML file:

<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}"/>

The Model file is :

public class TableModel {

    public String shopName = "lalala";
    ....
}

The Snippet works when it is just a one-way data binding (@{shopInfo.shopName}), but failed (cannot find the getter for attribute) if it is two-way binding.

I also found a question about this issue, but the answer didn't work for me.

//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 {
    ...
}

Is it a bug of IDE or dataBinding lib?

Community
  • 1
  • 1
2BAB
  • 1,215
  • 10
  • 18
  • The error mentions `MyTableRow`, but the class that has the getter is on `MyShopTableRow`. Make sure that you use MyShopTableRow in the layout file. – George Mount Sep 30 '16 at 20:38
  • @GeorgeMount, sorry for my ambiguous description and wrong code ( that's because I hide some sensitive infos, but the modification is not absolute right.) – 2BAB Oct 08 '16 at 02:58

1 Answers1

2

You did not define the event rowContentTextAttrChanged and when the event triggers, use which method to get the new value.

It should be something like

InverseBindingMethods({InverseBindingMethod(
      type = android.widget.TextView.class,
      attribute = "android:text",
      event = "android:textAttrChanged",
      method = "getText")})
  public class MyTextViewBindingAdapters { ... }
markzhai
  • 46
  • 1
  • 2
  • https://halfthought.wordpress.com/2016/03/23/2-way-data-binding-on-android/ ,inspired by your answer, I read that article again, and finally resolved the problem follow the article.Thanks. – 2BAB Oct 08 '16 at 02:49