3

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.

0

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome !!! Thanks for that great explanation. But why can't we pass arguments in data binding?
0

since you want to use a static method in DateUtil you should import it:

<data>
    <variable ... />
    <import type="foo.bar.DateUtil"/>
</data>

and in TextView:

<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"
               //use DateUtil directly-
                android:text="@{DateUtil.formatDate(comment.postedAt.time)}"/>

your error was trying to use it as a variable - this tells databinding to expect this class instance to be bound somewhere in your UI class (Fargment/Activity)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.