Can someone help me with this code? I am trying to create a recyclerview Adapter however in the override fun onBindViewHolder my textView and imageView has an error saying unresolved reference. However I stated them in the ViewHolder class.
class CNNRecyclerViewAdapter(data: List<VideoData>, val listener: RecyclerViewClickListener): RecyclerView.Adapter<ViewHolder>() {
var videos = data
set(value) {
field = value
notifyDataSetChanged()
}
interface RecyclerViewClickListener {
fun onItemClick(view: View, position: Int)
}
class ViewHolder(val view: View): RecyclerView.ViewHolder(view) {
val textView = view.findViewById<TextView>(R.id.textView)
val imageView = view.findViewById<ImageView>(R.id.imageView)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(
R.layout.cnn_list_item, parent, false)
return ViewHolder(view)
}
override fun getItemCount(): Int {
return videos.size
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
holder.textView.text = videos[position].title
holder.imageView.setImageBitmap(videos[position].cover)
holder.itemView.setOnClickListener {
listener.onItemClick(it, position)
}
}
}
I have the vals in the viewholder but it keeps having resolved reference
2
Answers
Your viewholder
ViewHolder(val view: View)
extends fromRecyclerView.ViewHolder(view)
You should change your function onBindViewHolder
to the following:
since it is in ViewHolder that your variables are defined.
I really dont know why yours it not working while mine is fine.
}