58

I am new to Kotlin development in android. here I am trying to access a variable defined in a class from it's inner class as below.

class MainActivity : AppCompatActivity() {

    var frags: MutableList<Fragment> = mutableListOf()

//.............onCreate and other methods ....

    internal class CustAdapter(var arrayList: ArrayList<NavigationData>) : RecyclerView.Adapter<CustAdapter.MyViewHolder>() {
    override fun onBindViewHolder(holder: MyViewHolder?, position: Int) {
        holder!!.bindItems(arrayList[position])
    }

    override fun getItemCount(): Int {
        return arrayList.size
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustAdapter.MyViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.navigation_item, parent, false)
        return MyViewHolder(v)
    }

    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bindItems(data: NavigationData) {


            itemView.setOnClickListener {
                   frags.add(BoardFrag()) ///// here i'm getting error "unresolved symbol"

            }
        }
    }
}    
}

inside inner class MyViewHolder it is not allowing me to access any variable of outer scope.

even I'm unable to access view ids imported from import kotlinx.android.synthetic.main.activity_main.* inside inner class methods.

I was able to access variables in such a way in java but i have read many question on stackoverflow but i didn't get answer yet.

2
  • 5
    Use inner, not internal =) Commented Sep 23, 2017 at 4:50
  • Use inner keyword instead of internal class parent { val m=1; inner class child{ print(m)} } Commented Sep 22, 2021 at 7:28

3 Answers 3

91

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your solution. This tiny mistake was bitting me from two days and you saved my another day
You are welcome! The name internal and inner is really tricky... Keep with protected would be better I guess
I do not know the difference between protected and internal and inner but i will try to sort it out on my own
52

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

class OuterClass{

var accessMe ="access me from Inner Class"

    inner class InnerClass{

       //....

   
        fun accessingOuterClassVariable(){

           accessMe = "Now the variable is accessed"

        }

    }

}

2 Comments

Where is the call-site for var accessMe?
@IgorGanapolsky Please check , I have updated the answer.
10

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

class OuterClass{

private var accessibleInside: CustomObject? = null
inner class InnerClass{

    //....
}

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.