android - 数据绑定(bind) : Resources$NotFoundException when attribute of object is int

标签 android data-binding binding

我正在尝试使用数据绑定(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/

相关文章:

angularjs - 一次绑定(bind): update model and re-render view

android - 从底部滑动 View 到另一个布局

android - 如何使用后台作业查找用户在间隔内访问的应用程序列表?

c# - 如何在同样数据绑定(bind)的代码中创建平铺图像?

c# - c# 如何从另一个线程更新datagridview的数据源

android - 如何解决 secret 文件绑定(bind)问题

java - Android 中的 JSON 数据加密和解密

android - 绕过AndroidX迁移错误而不禁用Lint

c# - 复选框绑定(bind)模式以一种方式工作

wpf - UpdateSourceTrigger 的 Default vs PropertyChanged?