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<String, Friend>" />
</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?
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.
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.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With