java 字符串转ingeter,双向绑定无法解析java.lang.String属性的setter

I am playing with the two-way binding of the data binding API which was introduced in Android Studio 2.1 AFIK.

I get this interesting error:

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

> java.lang.RuntimeException: Found data binding errors.

****/ data binding error ****msg:The expression address.street cannot cannot be inverted: Two-way binding cannot resolve a setter for java.lang.String property 'street'

file:/path/to/layout.xml

loc:34:37 - 34:50

****\ data binding error ****

When I try to google that error I just find a 4 day old Japanese Twitter posting from a guy who is crying about it...

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="@string/edit_hint_zip"

android:text="@={address.zip}"

tools:text="12345"/>

That address.zip is a String. I am guessing that the problem here is CharSequence vs. String as the return value of EditText.getText().

My idea was to defining it however this does not work for me:

@NonNull

@InverseBindingAdapter(attribute = "text")

public static String getText(EditText edit) {

return edit.getText().toString();

}

What did I miss?

解决方案

This bug is ugly as hell and properly a bug in the data binding API. The solution is to generate a setter and a getter. I came up fast with the idea to create a setter, but not to create a getter.

Here is now my simplified model:

public class Address {

public String street;

public void setStreet(String street) {

this.street = street;

}

public String getStreet() {

return street;

}

}

As you may note the getter and setter are useless, but required for two way binding.

If you think that this is a bug of the API please star my bug report: Two-way binding required setters AND ALSO getters

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值