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
}
)
}
}
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With