Android binding adapter passing multiple arguments causes an error

I am brand new in Android Data Binding . I follow this guide: Data Binding Library . I am trying to make an adapter that receives several parameters. This is my code:

XML

  <ImageView android:layout_width="@dimen/place_holder_size" android:layout_height="@dimen/place_holder_size" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_centerVertical="true" app:url="@{image.imageUrl}" app:size="@{@dimen/place_holder_size}" /> 

ADAPTER BINDING BUTTONS

 public class ViewBindingAdapters extends BaseObservable { @BindingAdapter({"bind:url", "bind:size"}) public static void loadImage(ImageView imageView, String url, int size) { if (!Strings.isNullOrEmpty(url)) { Picasso.with(imageView.getContext()).load(url).resize(size, size).centerCrop().into(imageView); } } .... } 

But I get this error:

java.lang.RuntimeException: Data binding errors detected. **** / data binding error **** msg: Unable to find the installer for the 'app: url' attribute with the type parameter java.lang.String on android.widget.ImageView. file: ... li_image_item.xml loc: 30: 27 - 30:40 **** \ data transfer error ****

Does anyone know why?

Thanks in advance!

+18
source share
4 answers

Task @dimen/place_holder_size , it returns float while you catch it as int

change the BindingAdapter method to

 @BindingAdapter({"bind:url", "bind:size"}) public static void loadImage(ImageView imageView, String url, float size) { } 

you can refer to

+26
source

try it

  @BindingAdapter(value={"url", "size"}, requireAll=false) public static void loadImage(ImageView imageView, String url, int size) { if (!Strings.isNullOrEmpty(url)) { Picasso.with(imageView.getContext()).load(url).resize(size, size).centerCrop().into(imageView); } } 
+4
source

I was wrong, this is the order of the arguments in the function. You can add multiple attributes to the Binding Adapter, but they must match the arguments with the same sequence defined in the method .

Here is my code snippet for Kotlin

 @BindingAdapter(value = ["bind:brand", "bind:model", "bind:age"], requireAll = false) @JvmStatic fun bindProductDetails(linearLayout: LinearLayout, brand: String?, model: String?, age: String?) { if (brand != null && !brand.isEmpty()) { //code //code } } 
+4
source

Refresh

You do not need to create a bind: prefix bind: just use this.

 @BindingAdapter({"url", "size"}) public static void loadImage(ImageView imageView, String url, float size) { } 

In XML, use any prefix, like app:

 app:url="@{image.imageUrl}" 
+2
source

Source: https://habr.com/ru/post/1012940/


All Articles