Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Binding adapter not working

I have create a binding adapter to display picture with picasso, but it doesn't work. I have the following error :

Found data binding errors. ****/ data binding error ****msg:Cannot find the setter for attribute 'app:loadPicture' with parameter type java.lang.String on android.widget.ImageView. file:/home/groupevsc.com/mathieu_labar/Documents/Projects/android-jetpack/app/src/main/res/layout/activity_detail_movie.xml loc:27:31 - 27:52 ****\ data binding error ****

Here is my binding adapter :

object CommonBindingUtil {

    @JvmStatic
    @BindingAdapter("loadPicture")
    fun loadPicture(view: ImageView, text: String) {
        Picasso.with(view.context)
                .load(text)
                .error(R.drawable.ic_movie_24)
                .fit()
                .placeholder(R.drawable.ic_movie_24)
                .into(view)
    }

}

And my XML has attribute "app:loadPicture" :

<ImageView
    android:id="@+id/picture"
    android:layout_width="@dimen/material_image_simple_width"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:src="@drawable/ic_movie_24"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:loadPicture="@{viewModel.movie.Poster}"/>

Here is my GitHub repository: https://github.com/mlabar/android-jetpack/tree/tech_ajout_databinding

Would anyone have an idea to solve my problem?

Thank you!

like image 296
MLabar Avatar asked Jul 19 '18 09:07

MLabar


People also ask

What is Android binding adapters?

Binding adapters are responsible for making the appropriate framework calls to set values. One example is setting a property value like calling the setText() method. Another example is setting an event listener like calling the setOnClickListener() method.

Is data binding good in Android?

Using data binding can lead to faster development times, faster execution times and more readable and maintained code. Android data binding generates binding classes at compile time for layouts.


2 Answers

Thank you @Blackbelt to solve my problem, i have added "kotlin-kapt" in all my build.gradle modules:

apply plugin: 'kotlin-kapt'
like image 50
MLabar Avatar answered Oct 19 '22 14:10

MLabar


Here is my code

@JvmStatic
    @BindingAdapter({"bind:loadPicture"})
    fun loadPicture(view: ImageView, loadPicture: String) {
        Picasso.with(view.context)
                .load(loadPicture)
                .error(R.drawable.ic_movie_24)
                .fit()
                .placeholder(R.drawable.ic_movie_24)
                .into(view)
    }

for more details see my project on GitHub

like image 24
fightingCoder Avatar answered Oct 19 '22 15:10

fightingCoder