Unable to access variable from innerclass : Kotlin android Unable to access variable from innerclass : Kotlin android android android

Unable to access variable from innerclass : Kotlin android


You should use the inner modifier in your adapter.

This modifier makes the inner class have access to the members of the outer class

Reference: https://kotlinlang.org/docs/reference/nested-classes.html


Define your nested class as inner then you will be able to access an outer class member variable.

class OuterClass{var accessMe ="access me from Inner Class"    inner class InnerClass{       //....        fun accessingOuterClassVariable(){           accessMe = "Now the variable is accessed"        }    }}


to answer this question for the fast an easy way i would do something like following :

class OuterClass{private var accessibleInside: CustomObject? = nullinner class InnerClass{    //....}

now the CustomObject could be anything from Context to String hope this helps someone.