Home > front end >  Assistance declaring RecyclerView.Adapter in Kotlin
Assistance declaring RecyclerView.Adapter in Kotlin

Time:12-17

Pre: first time declaring my viewHolder and recently using Kotlin. Maybe I have a very basic error, however I cannot see it. Ty in advance.

The error:

"Unresolved reference: ViewHolder"

happens when specifying my ViewHolder in the class declaration as follows:


package gmn.suninja.recyclerviewproject

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Adapter
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder

class Adapter: RecyclerView.Adapter<Adapter.ViewHolder>{ // ERROR here "ViewHolder"
    // Atributes of Adapter: list of users.
    private  var  userList: List<ModelClass>

    // Constructor
    constructor(userList: List<ModelClass> ){
        this.userList = userList
    }

    /* Implement the three methods from interface. */
    /* Inflates view and returns HeaderViewHolder. */
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Adapter.ViewHolder {// ERROR here "ViewHolder"
        val view: View = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_design, parent,false)
        return ViewHolder(view)
    }

    /* To bind the data from the Main Activity or Data class inside the RecyclerView*/
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        var resource:Int = userList.get(position).getImageView()
        var name: String = userList.get(position).getTextViewName()
        var message: String = userList.get(position).getTextViewMessage()
        var time:String = userList.get(position).getTextViewTime()

        // Now, to send this data (above) to the holder
        holder.setData(resource, name, message, time)
    }

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

    /* ViewHolder for displaying list. */
    // #1 #custonRecyclerView -> create ViewHolder
    class ViewHolder(val itemView: View): RecyclerView.ViewHolder(itemView) {
        fun setData(resource: Int, name: String, message: String, time: String) {
            ivImage.setImageResource(resource)
            tvName.setText(name)
            tvMessage.setText(message)
            tvTime.setText(time)

        }
        private var ivImage: ImageView  = itemView.findViewById(R.id.imageView)
        private var tvName: TextView    = itemView.findViewById(R.id.textViewName)
        private var tvMessage: TextView = itemView.findViewById(R.id.textViewMessage)
        private var  tvTime: TextView   = itemView.findViewById(R.id.textViewTime)



    }
}

I tried several sort of things. However the error takes place using a public Android interface "<Adapter.ViewHolder>". So, I don't know how to avoid that error from happening since I have to implement it.

I'm new at this. Thanks again.

I have found and tried how to do it in Java, and it works. But not in Kotlin so it is probably a very basic question >_<

CodePudding user response:

it is clashing/conflicting with your import line

import android.widget.Adapter

Either do

import android.widget.Adapter as AndroidAdapter

or rename your class to something better then "Adapter".

You basically declared your ViewHolder Class under the Class "Adapter", which has the same name as the one you import from the Android SDK, so it looks inside the class from Android for the class ViewHolder and not in your class named Adapter.

I hope this helps. :)

CodePudding user response:

The error of

"Unresolved reference: ViewHolder"

tells you that you are referring ViewHolder and the resource cannot be seen. You will need to import. You will need to import the necessary package (read this)

  • Related