Pass list of objects from one activity to other activity in android

后端 未结 7 1734
轻奢々
轻奢々 2020-12-01 01:35

I want to pass a list of objects from one activity from another activity. I have one class SharedBooking Below:

public class SharedBooking {         


        
7条回答
  •  萌比男神i
    2020-12-01 02:04

    First, make the class of the list implement Serializable.

    public class MyObject implements Serializable{}
    

    Then you can just cast the list to (Serializable). Like so:

    List list = new ArrayList<>();
    myIntent.putExtra("LIST", (Serializable) list);
    

    And to retrieve the list you do:

    Intent i = getIntent();
    list = (List) i.getSerializableExtra("LIST");
    

    That's it.

提交回复
热议问题