使用自定义转换器的两种方式数据绑定

unl*_*udo 5 android android-databinding android-viewmodel android-architecture-components

我想将数据绑定与视图模型一起使用,如此处所述

所以这里是摘录:

布局:

    <data class="FragmentEditPersonDataBinding">
    <import type="com.unludo.interview.persons.edit.Converter"/>

    <variable
        name="viewmodel"
        type="com.unludo.interview.persons.edit.PersonEditViewModel" />
   [...]
                 <EditText
                android:id="@+id/editBirthday"
                android:inputType="date"
                android:text="@={Converter.dateToString(viewmodel.birthday)}"
Run Code Online (Sandbox Code Playgroud)

转换器:

object Converter {
    @InverseMethod("stringToDate")
    @JvmStatic
    fun dateToString(
            view: EditText, oldValue: String,
            value: Date
    ): String {
        val sdf = SimpleDateFormat("dd/MM/yyyy", Locale.FRANCE)

        return sdf.format(value)
    }

    @JvmStatic   
    fun stringToDate(
            view: EditText, oldValue: String,
            value: String
    ): Date {
        val sdf = SimpleDateFormat("dd/MM/yyyy", Locale.FRANCE)

        return sdf.parse(value)
    }
}
Run Code Online (Sandbox Code Playgroud)

视图模型:

class PersonEditViewModel {
    var birthday: Date = GregorianCalendar(1993, 5, 19).time
    ...
Run Code Online (Sandbox Code Playgroud)

现在我在构建时收到此错误:

e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: 
    Found data binding errors.
****/ data binding error ****msg:cannot find method dateToString(java.util.Date) 
    in class com.unludo.interview.persons.edit.Converter 
[...]
 - 134:78 ****\ data binding error ****
Run Code Online (Sandbox Code Playgroud)

我正在使用最新的数据绑定 alpha,所以我想知道库中是否存在错误。

感谢您的帮助!

- - 更新

如果我像这样编写转换器,那么它会编译,但这与文档不符。知道为什么吗?

object Converter {

    @InverseMethod("stringToDate")
    @JvmStatic
    fun dateToString(
            value: Date
    ): String {
        val sdf = SimpleDateFormat("dd/MM/yyyy", Locale.FRANCE)

        return sdf.format(value)
    }
    @JvmStatic
    fun stringToDate(
            value: String
    ): Date {
        val sdf = SimpleDateFormat("dd/MM/yyyy", Locale.FRANCE)

        return sdf.parse(value)
    }
}
Run Code Online (Sandbox Code Playgroud)

Zal*_*ian 1

有点旧的线程,但我也一直在努力解决 2 路数据绑定问题,所以对于任何需要这个问题答案的人来说,问题是 Unlundo 按照记录的方式制作了他们的转换器,其中有一个视图,并且旧的,和一个新的价值。然而,这方面的文档并不是很清楚。

类型转换器中的参数也必须存在于布局文件中。对于布局中的原始绑定 ,android:text="@={Converter.dateToString(viewmodel.birthday)}"只有一个参数 - viewmodel.birthday,我们假设它是一个日期。因此,我们的类型转换器和逆转换器仅获得 1 个参数。

如果您对多个绑定重用同一个转换器,并且希望能够查看用户更改的视图,则可以使用布局中的 ID 将视图作为参数传递。这将传递生日以及用户正在编辑的视图:

                <EditText
                android:id="@+id/editBirthday"
                android:inputType="date"
                android:text="@={Converter.dateToString(edtBirthday, viewmodel.birthday)}"
Run Code Online (Sandbox Code Playgroud)

这也意味着您的类型转换器和逆转换器都需要在 EditText 的开头添加一个附加参数。该库似乎确实足够聪明,可以正确获取视图类型,而不仅仅是为您提供 aView作为您的参数,至少。

另外,如果您遇到转换器仅向字符串方向触发的问题,请确保您实际设置了绑定变量。如果布局绑定的变量是null,它将转换默认值以显示,但它将无法绑定回任何内容