Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Databinding Error: old values should be followed by new values. Parameter 2 must be the same type as parameter 3

I am using data binding for my custom fields. I have set a custom data binding adapter for this.

My Binding Adapter looks like this:

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

And in my layout file I have:

 ...
 <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}" />

But while running I am getting the below error:

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.

I don't understand what is going wrong here?

like image 914
kirtan403 Avatar asked Sep 07 '16 16:09

kirtan403


People also ask

Which is better ViewBinding and DataBinding?

The main advantages of viewbinding are speed and efficiency. It has a shorter build time because it avoids the overhead and performance issues associated with DataBinding due to annotation processors affecting DataBinding's build time.

Which code is correct to create custom adapters in data binding?

To create a custom binding adapter, you need to create an extension function of the view that will use the adapter. Then, you add the @BindingAdapter annotation. You have to indicate the name of the view attribute that will execute this adapter as a parameter in the annotation.

Can I use both data binding and view binding?

Data binding includes everything that ViewBinding has, so it wasn't designed to work side by side with View binding. The biggest issue is the naming conflict between the generated classes. Both ViewBinding and DataBonding would want to generate the class MainLayoutBinding for the layout main_layout. xml .


1 Answers

Error is within your BindingAdapter, it should be

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

}

all the values should be comma seperated and in double quotes.

like image 85
Ravi Avatar answered Sep 28 '22 04:09

Ravi