我正在尝试使用数据绑定(bind)。如果我使用具有字符串属性的对象,它可以正常工作,但在这种情况下,我使用 int 并且它不起作用。 我有对象用户:
public class User extends BaseObservable{
public int age;
......
public User() {}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
...
}
这是我的布局
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="user" type="com.example.bindingview.User"/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{user.age}"/>
</LinearLayout>
</layout>
问题是 TextView 不能有 age 的 int 文本。 如果我从 int 更改为 age 属性字符串,它工作正常。 我应该怎么做才能避免这个问题?
最佳答案
只需添加 String.valueOf():
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{String.valueOf(user.age)}"/>
关于android - 数据绑定(bind) : Resources$NotFoundException when attribute of object is int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32837455/