skip to Main Content

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


  1. Your viewholder ViewHolder(val view: View) extends from RecyclerView.ViewHolder(view)

    class ViewHolder(val view: View): RecyclerView.ViewHolder(view)
    

    You should change your function onBindViewHolder

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int)
    

    to the following:

    override fun onBindViewHolder(holder: ViewHolder, position: Int)
    

    since it is in ViewHolder that your variables are defined.

    Login or Signup to reply.
  2. I really dont know why yours it not working while mine is fine.

    class CNNRecyclerViewAdapter(
        var data: List<String> = emptyList(),
        val listener: RecyclerViewClickListener? = null
    ) : RecyclerView.Adapter<CNNRecyclerViewAdapter.ViewHolder>() {
    
    interface RecyclerViewClickListener {
        fun onItemClick(str: String)
    }
    
    inner class ViewHolder(rootView: View) : RecyclerView.ViewHolder(rootView) {
        val textView = rootView.findViewById<TextView>(R.id.textView)
    
        fun bindData(str: String) {
            textView.text = str
            textView.setOnClickListener {
                listener?.onItemClick(str)
            }
        }
    }
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.layout_item_item, parent, false)
        )
    }
    
    override fun getItemCount() = data.count()
    
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindData(data[position])
    }
    

    }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search