Home > Software engineering >  Recyclerview override issue in onBindViewHolder , Not overiding
Recyclerview override issue in onBindViewHolder , Not overiding

Time:12-28

Override function onBindViewHolder is not overwriting when I add ViewHolder class inside it. When I put viewHolder class outside it. it is not working.

Below in my code -

class data_custom_adapter(private val context: Context, private val datalist : ArrayList<Display_data>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.display_data_card, parent, false)
        return CustomAdapter.ViewHolder(v)
    }


    //Issue occur here -- it is not overriding when I add ViewHolder class
    override fun onBindViewHolder(holder: data_custom_adapter.ViewHolder, position: Int) {
        holder.bindItems(context,datalist[position])
    }

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

    //the class is holding the list view
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {


        private val datalist_data = itemView.findViewById(R.id.displayxmldata) as TextView


        fun bindItems(context: Context, datashow : Display_data) {
            datalist_data.text = datashow.appdata
        }

    }

}

Guys, please help to find out issue in this.

CodePudding user response:

The ViewHolder must match the class that you indicate in the first line. so in

class data_custom_adapter(private val context: Context, private val datalist : ArrayList<Display_data>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

there's CustomAdapter.ViewHolder. Change this to your data_custom_adapter.ViewHolder and it should work. so like

class data_custom_adapter(private val context: Context, private val datalist : ArrayList<Display_data>) : RecyclerView.Adapter<data_custom_adapter.ViewHolder>() {

this needs to be also changed in the onCreateViewHolder function

CodePudding user response:

I hope this will work.

class data_custom_adapter(
    private val context: Context,
    private val datalist : ArrayList<Display_data>) : RecyclerView.Adapter<data_custom_adapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.display_data_card, parent, false)
        return data_custom_adapter.ViewHolder(v)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindItems(context,datalist[position])
    }

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

    //the class is holding the list view
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {


        private val datalist_data = itemView.findViewById(R.id.displayxmldata) as TextView
        
        fun bindItems(context: Context, datashow : Display_data) {
            datalist_data.text = datashow.appdata
        }

    }
}
  • Related