android - 数据绑定(bind)错误 : old values should be followed by new values. 参数 2 必须与参数 3 的类型相同

标签 android data-binding android-databinding

我正在为我的自定义字段使用数据绑定(bind)。我为此设置了自定义数据绑定(bind)适配器。

我的绑定(bind)适配器如下所示:

@BindingAdapter({"created_by,created_at"})
public static void setDetailCreated(TextView textView, String createdBy, long createdAt) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(createdAt);

    SimpleDateFormat dateFormat = new SimpleDateFormat("h:mm a, dd MMM yyyy");

    String format = textView.getContext().getString(R.string.details_created, createdBy,
            dateFormat.format(cal.getTime()));

    textView.setText(format);
}

在我的布局文件中我有:

 ...
 <data>

    <import type="java.util.Map" />

    <import type="com.example.beans.Friend" />

    <variable
        name="user"
        type="com.example.beans.User" />

    <variable
        name="friends"
        type="Map&lt;String, Friend&gt;" />

 </data>

 ....
 ....

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:created_by="@{friends[user.createdBy].name}"
        app:created_at="@{user.createdAt}" />

但是在运行时出现以下错误:

Error:Execution failed for task ':app:compileDebugJavaWithJavac'. java.lang.RuntimeException: failure, see logs for details. BindingAdapter setDetailCreated(android.widget.TextView,java.lang.String,long): old values should be followed by new values. Parameter 2 must be the same type as parameter 3.

我不明白这里出了什么问题?

最佳答案

错误在你的BindingAdapter中,应该是

@BindingAdapter({"created_by","created_at"})
public static void setDetailCreated(TextView textView, String createdBy, long createdAt){

}

所有值都应该用逗号分隔并用双引号引起来。

关于android - 数据绑定(bind)错误 : old values should be followed by new values. 参数 2 必须与参数 3 的类型相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39374874/

相关文章:

Android 数据绑定(bind) : package does not exist

Android 应用重定向到自定义商店而不是 Google Play 商店

android - 跳跃动画的问题

android - 无法在gradle中解析com.melnykov:floatingactionbutton:1.3.0

c# - 异步加载 ViewModel 中的数据(使用 async 和 await)不使用数据绑定(bind)

c# - 如何在两个窗口之间的 Wpf 应用程序中保持 session ?

c# - 显示枚举描述而不是名称

android - 数据绑定(bind) Android - 类型参数 T 具有不兼容的上限 : ViewDataBinding and MainActivity

android - DataBinding ObserableField<String> 不更新 View

android - 适用于Android的OpenCV 2.4.6显示错误