Skip to content Skip to sidebar Skip to footer

Why Is This Bindingadapter Not Working In Kotlin?

I have a ViewModel with: val imageUrl = ObservableField() My layout XML has: I have a Binding

Solution 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'

Solution 2:

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

Model

classHabit(val title:String,val description:String,val image:Bitmap) {

and the Adapter

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

classHabitViewHolder(val card:View):RecyclerView.ViewHolder(card)

overridefunonBindViewHolder(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)
    }
}
    overridefunonCreateViewHolder(parent: ViewGroup, viewType: Int): HabitViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.single_card, parent,false)
        return HabitViewHolder(view)
    }

    overridefungetItemCount(): Int {
        return habits.size
    }
}

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

Post a Comment for "Why Is This Bindingadapter Not Working In Kotlin?"