首先,这个问题不是“onClick”事件参数传递的情况。
我有一个DateUtil类,其中有一个以下方法:
我收到的错误信息是:
所以我是否做错了什么或者这是一个bug?
请提供解决方案,以便在数据绑定中传递参数给方法。
public static String formatDate(long date) {
SimpleDateFormat dateFormat;
dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Calendar c = Calendar.getInstance();
dateFormat.setTimeZone(TimeZone.getDefault());
c.setTimeInMillis(date);
return dateFormat.format(c.getTimeInMillis());
}
我的模型 CommentEntity 具有以下属性:
private int id;
private int productId;
private String text;
private Date postedAt;
现在,在我的一个布局中,我正在显示评论。
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="comment"
type="com.example.entity.CommentEntity"/>
<variable
name="dateUtil"
type="com.example.util.DateUtil"/>
</data>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_alignParentRight="true"
android:layout_below="@id/item_comment_text"
//This line gives error for data binding
android:text="@{dateUtil.formatDate(comment.postedAt.time)}"/>
</layout>
我收到的错误信息是:
如果我修改formatDate()方法,使其默认使用当前时间,因此在数据绑定中删除参数传递,那么它将完美地工作。在类long中找不到方法 formatDate(com.example.util.DateUtil)
所以我是否做错了什么或者这是一个bug?
请提供解决方案,以便在数据绑定中传递参数给方法。