Q:

Why do I get unresolved reference in setOnClickListener?

I have this code:
class ViewHolder(view: View) : RecyclerView.ViewHolder(view){

    fun bindItem(items: Item) {
        itemView.name.text = items.name
        Glide.with(itemView.context).load(items.image).into(itemView.image)

        view.setOnClickListener {
            view.context.startActivity(view.context.intentFor<DetailsActivity>("image" to items.image, "name" to items.name))
        }
    }
}

Which keeps giving me an error that shows unsolved reference: view.
How to solve this? Thanks.

A:

It happens because you don't keep any reference to this view inside your class, what you do in Java terms is just super(view) call. You can get an access to the RecyclerView.ViewHolder#itemView field instead:
fun bindItem(items: Item) {
    itemView.name.text = items.name
    Glide.with(itemView.context).load(items.image).into(itemView.image)

    itemView.setOnClickListener {
        itemView.context.startActivity(itemView.context.intentFor<DetailsActivity>("image" to items.image, "name" to items.name))
    }
}

