Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't reach any class member from a nested class in Kotlin

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)         }     }} 
like image 403
ftibi93 Avatar asked Jul 31 '17 14:07

ftibi93


People also ask

How do I access nested class in Kotlin?

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.

How do you access members of an inner class?

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.

Can nested classes access private members?

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.

How do you call an inner class from another class?

Syntax : outerclass. innerclass = new outerclass. innerclass();

How to access the member function of nested class in Kotlin?

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.

How are Kotlin classes similar to Java classes?

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.

Can a nested class access the members of its outer class?

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.

Can you use interfaces with nested 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:


1 Answers

In Kotlin, nested classes cannot access the outer class instance by default, just like nested static classes 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

like image 150
hotkey Avatar answered Oct 12 '22 00:10

hotkey