Unresolved reference kotlin variable


#1

I’m new to kotlin. I’m using the following code in my Adapter:

class TravelListAdapter(private var context: Context) : RecyclerView.Adapter<TravelListAdapter.MyViewHolder>() {

    lateinit var onClickListener: TravelListAdapterListener
    lateinit var holderView: View

    interface TravelListAdapterListener {
        fun onClickListener(v: View, position: Int)
    }


     class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        private lateinit var txtLabel: TextView

        fun MyViewHolder(view: View) {

            holderView = view // holderView shows error

            // in line below, holderView and onClickListener shows error
            holderView.setOnClickListener(
                View.OnClickListener { v -> onClickListener.onClickListener(v, adapterPosition) }
            )
        }

    }

    ...
    ...
}

The variables onClickListener and holderView have been declared in the Adapter class, but give this error when I access them in another class ( which is defined inside the Adapter class ):

Unresolved reference: variable_name

Can anyone tell me what I’m doing wrong ??


#2

classes without inner are static in Kotlin.
So, add ìnner before your inner class.

https://kotlinlang.org/docs/reference/nested-classes.html#nested-and-inner-classes


#3

Thanks @tieskedh !! works now :slight_smile: