5

Getting stuck imageview databindig below are my custom adapter and Imageview. I refer to this [https://stackoverflow.com/questions/40188894/cannot-find-the-setter-for-attribute-with-parameter] but not getting solution - can anyone help? Thanks.

@BindingAdapter("app:image_url")
fun loadImage(view: ImageView, logoUrl: String?) {
    if (logoUrl == null) {
        view.setImageResource(R.drawable.alert_dark_frame)
    } else {
        Picasso.with(view.getContext())
            .load(logoUrl)
            .placeholder(R.mipmap.sym_def_app_icon)
            .into(view)
    }
}

<data>
    <import type="android.view.View" />
    <variable name="abc"
              type="com.example.viewmodel.Result"/>
</data>

<ImageView
            android:id="@+id/circleImageView"
            android:layout_width="100dp"
            android:layout_height="100dp"
            app:image_url="@{abc.picture.thumbnail}"
            />
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
DevMobApp
  • 115
  • 1
  • 2
  • 10

4 Answers4

12

Can you try adding the plugin in your build.gradle (app) file

apply plugin: 'kotlin-kapt'
rafa
  • 1,319
  • 8
  • 20
  • By using this i got issue in Data-binding generated classes – DevMobApp Jan 14 '19 at 07:22
  • What's the issue which you are getting? – rafa Jan 14 '19 at 07:24
  • sorry for the late reply i am getting this error after using kotlin-kapt plugin ActivityMain2Binding(DataBindingComponent _bindingComponent, View _root, ^ symbol: class DataBindingComponent location: class ActivityMain2Binding – DevMobApp Jan 16 '19 at 12:37
  • I got a problem about this, and I add apply plugin: 'kotlin-kapt' this in my build.gradle,works fine. before I add this line , it don't even generate @BindingAdapter attr in whole project.thanks very much – Freddy Jan 18 '19 at 11:41
6

This is normal error because the class ImageView doesn't contain a method setImageUrl, so you need to do a workaround for that by creating a class that extends ImageView and contains a method named setImageUrl that takes a String as parameter and set the image inside that method using Picasso library:

public class MyImageView extends ImageView {

    // ... here is the constructors
    public void setImageUrl(String url) {
        Picasso.get().load(url).into(this);
    }
}

and after that you can use it in the xml like this:

<com.yourPackage.MyImageView
    android:id="@+id/circleImageView"
    android:layout_width="100dp"
    android:layout_height="100dp"
    app:image_url="@{abc.picture.thumbnail}"
    />
Khalid Taha
  • 3,183
  • 5
  • 27
  • 43
  • the one way which i found is i use picasso in onBindViewHolderlike below ( i know this is not right way ) Picasso.with(c) .load(holder.binding.abc!!.picture.medium) .placeholder(R.mipmap.ic_launcher) It solve my problem for temp. .into(holder.binding.circleImageView) – DevMobApp Jan 14 '19 at 07:19
  • i thought that when we create custom attributes to widgets, we only need to declare BindingAdapter or InverseBindingAdapter. it seems we also need to extend the widget itself as a class and declare those get/set methods? – chitgoks Oct 07 '19 at 06:35
2

I just changed from

app:srcCompat

to

android:src

in my case when using it together with databinding.Working fine now.Hope this helps.

OLD CODE

<androidx.appcompat.widget.AppCompatImageView
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:textSize="16sp"
                android:fontFamily="@font/avenir_roman"
                android:textColor="@color/textColor"
                app:srcCompat="@{model.image}"/>

NEW CODE

  <androidx.appcompat.widget.AppCompatImageView
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:textSize="16sp"
                    android:fontFamily="@font/avenir_roman"
                    android:textColor="@color/textColor"
                    android:src="@{model.image}"/>
Tandoh Anthony Nwi-Ackah
  • 2,135
  • 2
  • 16
  • 30
1

Write the following code in the model class

 companion object DataBindingAdapter {
        @BindingAdapter("bind:image_url")
        @JvmStatic
        fun loadImage(imageView: ImageView, image_url: String) {
            Picasso.get().load(image_url).fit()
                    .placeholder(R.mipmap.ic_launcher)
                    .error(R.mipmap.ic_launcher)        
                    .into(imageView)        }
    }

In XML write the following code

<ImageView
            android:id="@+id/movie_image"
            android:layout_width="100dp"
            android:layout_height="100dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.027"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:image_url="@{movieList.component5()}"/>
Snehal
  • 543
  • 7
  • 11