简体   繁体   中英

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!

谢谢@Blackbelt 解决了我的问题,我在所有build.gradle模块中添加了“kotlin-kapt”:

apply plugin: 'kotlin-kapt'

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

Change your MovieViewModel class like this:

class MovieViewModel(private val movie: Movie) : Observer, BaseObservable() {

    init {
       Movie.addObserver(this)
    }
    val Poster: String
        @Bindable get() {
            return Poster.name
        }
}

And Movie class like this:

class Movie: Observable() {
    var Title: String = ""
        set(value) {
            field = value
            setChangedAndNotify("Title")
        }
    var Year: String = ""
        set(value) {
            field = value
            setChangedAndNotify("Year")
        }
    var imdbID: String = ""
        set(value) {
            field = value
            setChangedAndNotify("imdbID")
        }
    var Type: String = ""
        set(value) {
            field = value
            setChangedAndNotify("Type")
        }
    var Poster: String = ""
        set(value) {
            field = value
            setChangedAndNotify("Poster")
        }
    var Plot: String = ""
        set(value) {
            field = value
            setChangedAndNotify("Plot")
        }

    private fun setChangedAndNotify(field: Any) {
        setChanged()
        notifyObservers(field)
    }
}

Make your class context according to your need, run it hope it'll solve your problem.

You are not passing the right parameters for binding adapter function.

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

}

Here loadPicture function expects parameters imageView and a String, but here, in your xml your are passing the Object of photo

app:loadPicture="@{viewModel.movie.Poster}

You should instead write something like this app:loadPicture="@{viewModel.movie.Poster.url}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM