Hi all, trying to set up my first ‘RecyclerView’ a...
# getting-started
w
Hi all, trying to set up my first ‘RecyclerView’ and a little confused as to why the View in class ViewHolder is unrecognized… noob to Kotlin and Android development, so trying to wrap my head around it
Copy code
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

class EntryAdapter(private val context: SpeechPageActivity, private val entryList: Array<String>) : RecyclerView.Adapter<EntryAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EntryAdapter.ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.entry_cell, parent, false))
    }

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

    override fun onBindViewHolder(holder: EntryAdapter.ViewHolder, position: Int) {
        holder.entryText?.text = entryList[position]
        holder.itemView.setOnClickListener {
            println(entryList[position])
        }
    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val entryText = view.entryText
        val entryImage = view.entryImage
    }

}
a
is not necessary to pass
context
as parameter in the Adapter, you can get it from the onCreateViewHolder as
parent.context
and here's a basic example of an recyclerView implementation https://androidwave.com/recyclerview-kotlin-tutorial/
r
You can’t assign directly this
Copy code
val entryText = view.entryText
        val entryImage = view.entryImage
create a function for that
or init
init {
…..
}
Copy code
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    var entryText
    var entryImage
    
    init {
        entryText = view.entryText
        entryImage = view.entryImage
     }
}
p
The way he’s initializing those properties is fine. I’m guessing it’s just that View isn’t imported
#android
w
@pt you’re right - looks like i needed to import the view from my class. But everyone was super helpful - nearly there, now just need to figure out how to dynamically assign @drawables based on entryText