Why is this BindingAdapter not working in Kotlin?

 ̄綄美尐妖づ 提交于 2019-12-19 03:19:29

问题


I have a ViewModel with:

val imageUrl = ObservableField<String>()

My layout XML has:

<ImageView
    ...
    app:url="@{viewModel.imageUrl}"
    .../>

I have a BindingAdapters file with a top level function:

@BindingAdapter("url")
fun loadImage(view: ImageView, url: String?) {
    ...
}

I'm getting the following error:

data binding error msg:Cannot find the setter for attribute 'app:url' with parameter type android.databinding.ObservableField<java.lang.String> on android.widget.ImageView.

Any idea why this would be? This is pretty much identical to how I set up binding adapters in Java, minus the static function.


回答1:


This issue was caused by a lack of kapt in the app's build.gradle. Adding the below to build.gradle and replacing all "annotationProcessor" dependencies with "kapt" fixes the issue.

apply plugin: 'kotlin-kapt'



回答2:


We do not work with images that much but here is our code

Model

class Habit(val title:String,val description:String,val image:Bitmap) {

and the Adapter

class HabitsAdapter(val habits:List<Habit>): RecyclerView.Adapter<HabitsAdapter.HabitViewHolder>() {

class HabitViewHolder(val card:View):RecyclerView.ViewHolder(card)

override fun onBindViewHolder(holder: HabitViewHolder, index: Int) {

    if(holder != null){
        val habit = habits[index]
        holder.card.tvTitle.text = habit.title
        holder.card.tvDescription.text = habit.description
        holder.card.ivINsingle_card.setImageBitmap(habit.image)
    }
}
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HabitViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.single_card, parent,false)
        return HabitViewHolder(view)
    }

    override fun getItemCount(): Int {
        return habits.size
    }
}



回答3:


I've had a different problem. This may not be the solution for your problem, but maybe others will benefit from it. What I did in plus was to add a placeholder and an error drawable as well to my load image function like this:

@JvmStatic
@BindingAdapter("imageUrl", "error", "placeholder")
fun ImageView.setImageFromUrl(imageUrl: String, error: Drawable?, placeholder: Drawable?) {
    Picasso.get()
            .load(imageUrl)
            .placeholder(placeholder
                    ?: ContextCompat.getDrawable(context, R.drawable.ic_general_placeholder)!!)
            .error(error
                    ?: ContextCompat.getDrawable(context, R.drawable.ic_general_error)!!)
            .into(this)
}

I tought that putting the ? to the end of the Drawables was enough and in the layout I could just provide the image URL with app:imageUrl, if I didn't want to provide any specific error and placeholder images. But I was wrong, I kept getting the same error as you, Cannot find setter... After a few hours of clearing the cache, deleting every build folder, restarting android studio a few times, I found the solution in a google provided guide. You need to specify inside the @BindingAdapter if all of the attributes are required or not. So I modified my code like this and it worked:

@JvmStatic
@BindingAdapter(value = ["imageUrl", "error", "placeholder"], requireAll = false)
fun ImageView.setImageFromUrl(imageUrl: String, error: Drawable?, placeholder: Drawable?) {
    Picasso.get()
            .load(imageUrl)
            .placeholder(placeholder
                    ?: ContextCompat.getDrawable(context, R.drawable.ic_general_placeholder)!!)
            .error(error
                    ?: ContextCompat.getDrawable(context, R.drawable.ic_general_error)!!)
            .into(this)
}


来源:https://stackoverflow.com/questions/52655607/why-is-this-bindingadapter-not-working-in-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!