How to reference an anonymous inner class from the outer class?

Is there a way to reference this inner class from the outer class? I’m trying to create a RecyclerView adapter on the fly like this:

rvItemsList.adapter = object : RecyclerView.Adapter<ViewHolderInnerClass>() {

    inner class ViewHolderInnerClass(view: View) : RecyclerView.ViewHolder(view) {
        var title: TextView = view.findViewById(R.id.title) as TextView
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): RecyclerView.ViewHolder {
        return ViewHolderInnerClass( LayoutInflater.from(parent.context).inflate(android.R.layout.simple_list_item_1, parent, false))
    }

    override fun getItemCount(): Int {
        return itemList.size // Reference to List<String> object outside this anonymous class
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        holder.title.text =  itemList[position]
    }
}

This above code doesn’t work because Kotlin doesn’t allow referencing the inner class ViewHolderInnerClass the way I did in Line 1 but how would I go about doing it correctly? Right now I’m using it like this.

Have you tried:

class MyAdapter : RecyclerView.Adapter<MyAdapter.ViewHolderInnerClass>() {
    .
    .
    .
}
rvItemsList.adapter = MyAdapter()

I figure it’s the anonymous outer class that is giving you the trouble.

I’ve used this way several times. However, now the outer class is no longer anonymous as it declares itself and then it’s assigned like:

rvItemsList.adapter = MyAdapter()

I’m hoping more of a solution where I can directly reference an inner class in anonymous manner. Something like:

rvItemsList.adapter = object : RecyclerView.Adapter<this::ViewHolderInnerClass>() {
    inner class ViewHolderInnerClass(view: View) : RecyclerView.ViewHolder(view) {
        var title: TextView = view.findViewById(R.id.title) as TextView
    }
}

I suppose Kotlin doesn’t have such a provision. hmm.