Search code examples
androidkotlinandroid-recyclerviewandroid-viewbinding

Android Kotlin viewBinder - change ItemView of RecyclerView Item from Activity


After I switched from Kotlin Synthetics to viewBinder I can't change an itemView of a RecyclerView item from activity.

This is inside activity:

val viewHolder = binding.recyclerViewMemes.findViewHolderForAdapterPosition(i) as RecyclerView.ViewHolder
viewHolder.itemView.memeCommentsCount.text = "\u25CF"

I get unresolved reference on memeCommentsCount

This is the ViewHolder inside Recyclerview:

class ViewHolder(itemView: MemeViewBinding?) : RecyclerView.ViewHolder(itemView!!.root){
    val memeCommentsCount = itemView!!.memeCommentsCount
}

Solution

  • It looks like you have your own implementation of ViewHolder but are casting your variable to the default implementation which does not have memeCommentsCount.

    Instead of as RecyclerView.ViewHolder try as ViewHolder and make sure you're importing your ViewHolder class and not the default one

    Alternatively rename your ViewHolder to something else like MemeViewHolder and cast your viewHolder variable to that

    Also you are trying to get it off the itemView of the view holder when you made it a variable on the view holder itself so this line needs to change from

    viewHolder.itemView.memeCommentsCount.text = "\u25CF"

    to

    viewHolder.memeCommentsCount.text = "\u25CF"