Help passing an ArrayList of Objects to a new Activity

前端 未结 10 2200
眼角桃花
眼角桃花 2020-11-28 08:53

I have an arraylist of objects. ie ArrayList.

I want to pass this to a new Activity. I tried to use putParcelableArrayList but it has issues with the object. I remov

10条回答
  •  自闭症患者
    2020-11-28 09:19

    You have two options:

    • Objects in the ArrayList put implement Parceable as required by the method putParcelableArrayList ()

    • Or the objects can implement Serializable and use the method putSerializable() to add the ArrayList, ie bundle.putSerializable("arraylist", arraylist);

    Android passes Object in Bundle by serializing and deserializing (via Serializable or Parcelable). So all of your Objects you want to pass in a Bundle must implement one of those two interfaces.

    Most Object serialize easily with Serializable and it is much simpler to implement Serializable and add a static UID to the class than to write all the methods/classes required for Parcelable.

提交回复
热议问题