Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing List of Objects to Fragment

class CholesterolPagingFragment: Fragment() {

    companion object {
        fun newInstance(): CholesterolPagingFragment {
            val args = Bundle()
            val fragment = CholesterolPagingFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_paging_cholesterol, container, false)

        return view
    }

}

I have wrote the above code in Kotlin to initialize a fragment. Though I can't figure out a way to pass a list of objects(eg: List<Human>) to this fragment. I have tried with the Bundle() but couldn't find a proper way.

like image 419
kokilayaa Avatar asked Dec 26 '17 09:12

kokilayaa


People also ask

How to pass arguments from one fragment to another fragment?

You should create a method within your fragment that accepts the type of object you wish to pass into it. In this case i named it "setObject" (creative huh? :) ) That method can then perform whatever action you need with that object. Show activity on this post. Passing arguments by bundle is restricted to some data types.

How to add an object to an activity fragment?

In fragment : Create a reference variable for the class whose object you want in the fragment. Simply create a setter method for the reference variable and call the setter before replacing fragment from the activity. 2. In activity: Show activity on this post. First store the objects in Bundle, don't forget to put implements serializable in class.

How to replace a fragment with a reference variable?

Simply create a setter method for the reference variable and call the setter before replacing fragment from the activity. 2. In activity: Show activity on this post. First store the objects in Bundle, don't forget to put implements serializable in class.

What is the use of serialization in list passing?

In this technique, we have used the Serialization for the list passing. Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed.


1 Answers

You can pass List<Human> as Parcelable ArrayList to the Bundle as follows:

 companion object {
    fun newInstance(myList : ArrayList<Human>): MyFragment {
        val args = Bundle()
        args.putParcelableArrayList("list",myList);
        val fragment = MyFragment()
        fragment.arguments = args
        return fragment
    }
}

To retrieve data:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val args = arguments
    var myList : ArrayList<Human> = args.getParcelableArrayList<Human>("list")
}

Do Parcelable implementation in Human Data Class :

data class Human(val name: String, val phone: String) : Parcelable {
    constructor(parcel: Parcel) : this(
            parcel.readString(),
            parcel.readString()) {
    }

    override fun writeToParcel(dest: Parcel?, flags: Int) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun describeContents(): Int {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    companion object CREATOR : Parcelable.Creator<Human> {
        override fun createFromParcel(parcel: Parcel): Human {
            return Human(parcel)
        }

        override fun newArray(size: Int): Array<Human?> {
            return arrayOfNulls(size)
        }
    }
}
like image 185
Yamini Balakrishnan Avatar answered Oct 02 '22 17:10

Yamini Balakrishnan