Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Data Binding with parameter passing to method

First, this question is not a case of 'onClick' event parameter passing.

I have DateUtil class with one method as below :

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());
    }

My model CommentEntity has following attributes :

 private int id;
 private int productId;
 private String text;
 private Date postedAt;

Now in one of layout I'm displaying the Comments.

<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>

The error I'm getting is :

cannot find method formatDate(com.example.util.DateUtil) in class long

Now for the same if I modify formatDate() method as it will take current time by default, hence removing the parameter passing in data binding, it will work perfectly.

So am I doing something wrong or is it a bug?

Please provide solution for problem to pass the parameter to method in data binding.

like image 288
Parikshit Chalke Avatar asked Aug 29 '26 06:08

Parikshit Chalke


1 Answers

Try below approach:

  1. Don't take your DateUtil class object direct from data binding in xml

Make one BindingAdapter method in your CommentEntity model class like this:

@BindingAdapter("android:text")
public static void setPaddingLeft(TextView view, long date) {
    view.setText(DateUtil.formatDate(long));
}

Then use like below for xml:

<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"
            android:text="@{comment.postedAt.time}"/>

Explanation :

When you wanted to apply some custom logic for your view based on data model, you need to use BindingAdapter to do that job. So, you can provide some custom tag or use any default android: tag on which you need to set logic.

I denied you using DateUtil for binding adapter because, you might be using it's methods to somewhere else too. So suggested to make new method in your model instead for that logic, so that core logic remains untouched. (You can use your DateUtils for this logic though, you just need to make it as BindingAdapter).

like image 157
Jeel Vankhede Avatar answered Aug 31 '26 20:08

Jeel Vankhede