I want to access a member of the MainFragment class from PersonAdapter class but none of them are available. I tried making both the classes and the members public and private also but so far nothing worked. I guess I'm missing something obvious but I just can't figure it out.
class MainFragment : Fragment() { lateinit var personAdapter: PersonAdapter lateinit var personListener: OnPersonSelected private var realm: Realm by Delegates.notNull() lateinit var realmListener: RealmChangeListener<Realm> override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val v = inflater.inflate(R.layout.fragment_main, container, false) return v } class PersonAdapter() : RecyclerView.Adapter<ViewHolder>() { var localPersonList = personList override fun onBindViewHolder(holder: ViewHolder, position: Int) { holder.bindItems(localPersonList[position]) holder.itemView.setOnClickListener { Toast.makeText(context, "click", Toast.LENGTH_SHORT).show() //I want to reach personListener from here } } override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { val v = LayoutInflater.from(parent!!.context).inflate(R.layout.person_list_item, parent, false) return ViewHolder(v) } }}
In Kotlin, to access the member function of nested class, we need to create the object for nested class and call the member function using it.
Accessing the Private Members Write an inner class in it, return the private members from a method within the inner class, say, getValue(), and finally from another class (from which you want to access the private members) call the getValue() method of the inner class.
A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private.
Syntax : outerclass. innerclass = new outerclass. innerclass();
Note: Nested class can’t access the members of the outer class, but we can access the property of nested class from the outer class without creating an object for nested class. In Kotlin, to access the member function of nested class, we need to create the object for nested class and call the member function using it.
Kotlin classes are much similar to Java classes when we think about the capabilities and use cases, but not identical. Nested in Kotlin is similar to a static nested class in Java and the Inner class is similar to a non-static nested class in Java.
A nested class marked as inner can access the members of its outer class. Inner classes carry a reference to an object of an outer class: Copied! See Qualified this expressions to learn about disambiguation of this in inner classes.
You can also use interfaces with nesting. All combinations of classes and interfaces are possible: You can nest interfaces in classes, classes in interfaces, and interfaces in interfaces. Copied! A nested class marked as inner can access the members of its outer class. Inner classes carry a reference to an object of an outer class:
In Kotlin, nested classes cannot access the outer class instance by default, just like nested static class
es can't in Java.
To do that, add the inner
modifier to the nested class:
class MainFragment : Fragment() { // ... inner class PersonAdapter() : RecyclerView.Adapter<ViewHolder>() { // ... } }
Note that an inner
class holds a reference to its containing class instance, which may affect the lifetime of the latter and potentially lead to a memory leak if the inner
class instance is stored globally.
See: Nested classes in the language reference
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