Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin: How can an inner class get access to a variable declared as a parameter in an outer class?

Tags:

android

kotlin

I have a DetailAdapter class with a parameter categoryId of type String. I need access to this variable in the inner class DetailHolder. I get the following error:

Unresolved reference: categoryId

How can I solve this?

class DetailAdapter(lifecycleOwner: LifecycleOwner, categoryId: String) : FirebaseRecyclerAdapter<Detail, DetailAdapter.DetailHolder>(DetailAdapter.buildOptions(lifecycleOwner, categoryId)) {

companion object {
    private fun buildQuery(categoryId: String) = FirebaseDatabase.getInstance()
            .reference
            .child("").child("details").child(categoryId)
            .limitToLast(50)

    private fun buildOptions(lifecycleOwner: LifecycleOwner, categoryId: String) = FirebaseRecyclerOptions.Builder<Detail>()
            .setQuery(buildQuery(categoryId), Detail::class.java)
            .setLifecycleOwner(lifecycleOwner)
            .build()
}

class DetailHolder(val customView: View, var detail: Detail? = null) : RecyclerView.ViewHolder(customView) {
    private val TAG = DetailHolder::class.java.simpleName

    fun bind(detail: Detail) {
        with(detail) {
            customView.textView_name?.text = detail.detailName
            customView.textView_description?.text = detail.detailDescription

            val detailId = detail.detailId

            customView.setOnClickListener {
                    // do something
            }

            customView.setOnLongClickListener(
                    {
                        showDeleteDetailDialog(it, categoryId, detailId)
                        true
                    }
            )
        }
    }
like image 698
CEO tech4lifeapps Avatar asked Apr 12 '18 14:04

CEO tech4lifeapps


People also ask

How do you access the inner class variable in Kotlin?

Kotlin Inner Class When we can declare a class inside another class using the keyword inner then it is called inner class. With the help of the inner class, we can access the outer class property inside the inner class. In the below program we are trying to access str from the inner class member function.

Can inner class access outer class private variables Kotlin?

Kotlin Inner class The advantage of inner class over nested class is that, it is able to access members of outer class even it is private.

Can inner class use variables from outer class?

Any non-static nested class is known as inner class in java. Java inner class is associated with the object of the class and they can access all the variables and methods of the outer class. Since inner classes are associated with the instance, we can't have any static variables in them.


1 Answers

DetailHolder in your code is a nested class, not an inner one (its equivalent in Java would be a static nested class).

To define an inner class you need to use the inner keyword:

inner class DetailHolder( ...

This way DetailHolder will hold an implicit reference to its enclosing class (DetailAdapter) and you'll be able to access its properties as well.

Check out the documentation on Nested and Inner Classes.

like image 149
earthw0rmjim Avatar answered Oct 22 '22 04:10

earthw0rmjim