【问题标题】:Data Binding Library create wrong type in auto generated java class数据绑定库在自动生成的 java 类中创建错误的类型
【发布时间】:2017-07-05 17:02:09
【问题描述】:

我有一个这样的 POJO 类(我删除了不相关的字段)

public class FeedingTable {

    private String Day;

    private Integer Fasting; <-- note that this field is Integer
}

并在 Layout xml 中使用它:

<data>

    <variable
        name="friday"
        type="models.FeedingTable" />
</data>

<!-- other views -->
<EditText
    android:id="@+id/fasting"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:inputType="number"
    android:text="@={friday.fasting}">
<!-- other views -->

当我编译我的项目时,我遇到了编译时错误,我检查了它并找出了为此布局生成的 java 类为 EditText 创建了一个 Short 字段,它在将 Short 转换为时遇到问题Integer.

// somewhere in generated java class
private android.databinding.InverseBindingListener fridayFastingandroidTextAttrChanged = new android.databinding.InverseBindingListener() {
    @Override
    public void onChange() {
        // Inverse of friday.fasting
        //         is friday.setFasting((java.lang.Integer) callbackArg_0)
        java.lang.Short callbackArg_0 = utilities.BindingUtils.getShortText(fridayFasting);
        // localize variables for thread safety
        // friday
        models.FeedingTable friday = mFriday;
        // friday.fasting
        java.lang.Integer fridayFasting = null; <-- ???????!!!!!!
        // friday != null
        boolean fridayJavaLangObjectNull = false;



        fridayJavaLangObjectNull = (friday) != (null);
        if (fridayJavaLangObjectNull) {




            friday.setFasting(((java.lang.Integer) (callbackArg_0))); <-- in this line I have error
        }
    }
};

为什么会这样?我尝试清理和重建项目,但没有成功。为什么数据绑定器创建 Short 而不是 Integer?如果你看到它是创建 java.lang.Integer fridayFasting = null; 但永远不要使用它。我对数据绑定器的工作原理感到非常困惑!

【问题讨论】:

    标签: java android android-databinding 2-way-object-databinding


    【解决方案1】:

    您已经认识到 String 和 Integer 之间没有自动转换,并为 Short 添加了双向绑定。我想它是这样的:

    package utilities;
    public class BindingUtils {
        @InverseBindingAdapter(attribute = "android:text")
        public static Short getShortText(TextView view) {...}
    }
    

    但是没有从 Short 到 Integer 的转换。 Data Binding 有点困惑,因为有一个从 short 到 int 的强制转换,并尝试使用直接强制转换来解决问题。

    如果您使用的是 Android Studio 3.0,则可以使用 InverseMethods 进行转换。你会使用这样的东西:

    public class BindingUtils {
        @InverseMethod("stringToInteger")
        public static String intToString(Integer value) { ... }
    
        public static Integer stringToInteger(String value) { ... }
    }
    

    您可以阅读更多关于他们的信息here

    在 Android Studio 2.3 上,您必须在 InverseBindingAdapter 中使用正确的类型,并且不能依赖数据绑定来进行类转换。

    public class BindingUtils {
        @InverseBindingAdapter(attribute = "android:text")
        public static Integer getIntText(TextView view) {...}
    
        @BindingAdapter("android:text")
        public static void setIntText(TextView view, Integer value) { ... }
    }
    

    【讨论】:

    • 是的,没错,我之前为Short 创建了一个InverseBindingAdapter。在您说之前我发现了问题,并为Integer 添加了另一个InverseBindingAdapter,这正是您提供的。但它会导致另一个问题。我在post 中提到了这一点。有时间请看一下。
    猜你喜欢
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 2021-09-08
    • 1970-01-01
    • 2014-11-27
    • 2011-06-23
    相关资源
    最近更新 更多