我希望使用数据绑定和视图模型,如此这里所解释的。
因此,以下是摘录:
布局:
<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)}"
转换器:
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)
}
}
视图模型:
class PersonEditViewModel {
var birthday: Date = GregorianCalendar(1993, 5, 19).time
...
现在编译时出现以下错误:
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 ****
我正在使用最新的数据绑定 alpha 版本,所以我想知道库中是否可能存在 bug。
感谢任何帮助!
--- 更新
如果我像这样编写转换器,则可以编译,但这与文档不符。有什么想法吗?
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)
}
}
dateToString
。 - Jeel Vankhedecom.unludo.interview.persons.edit
中有此对象类,请尝试在对象文件开头添加此注释:@file:JvmName("Converter")
。 - Jeel Vankhede