Passing ArrayList with objects to new Activity?

前端 未结 6 636
猫巷女王i
猫巷女王i 2020-12-19 02:42

I\'m trying to pass an ArrayList from my first Activity to the next one. Basically, the first activity parses an XML file and creates an ArrayList with objects inside. What

相关标签:
6条回答
  • 2020-12-19 03:03

    I made this trick to send from first Activity to second.

    The first activity

    ArrayList<String> mylist = new ArrayList<String>();  
    Intent intent = new Intent(ActivityName.this, Second.class);
    intent.putStringArrayListExtra("key", mylist);
    startActivity(intent);
    

    The second activity

    To retrieve

    ArrayList<String> list =  getIntent().getStringArrayListExtra("key");
    
    0 讨论(0)
  • This could be totally bad practice and I wouldn't know any better, but you could declare the ArrayList as public and static. Then just access it with Activity.ArraylistName.

    0 讨论(0)
  • 2020-12-19 03:23

    You can pass a String List using putStringArrayListExtra(String name, ArrayList<String> value) if it is strings. Or, you could serialize List and then use putExtra(String name, Serializable value).

    If you don't want to/can't use the above, you could use a central util class with a static reference to the List. Just set it in your first Activity and get it in the second.

    0 讨论(0)
  • 2020-12-19 03:24

    I would do a toString on the array and pass it as an extra by doing a intent.putExtra("label". array.toString()); and then just recover it in the new activity.

    0 讨论(0)
  • 2020-12-19 03:25

    You can set the scope of ArrayList at application level or you can do this using parcelable.

    0 讨论(0)
  • 2020-12-19 03:26

    Complex types may be passed by means of Parcelable. An example is in this question:

    Help with passing ArrayList and parcelable Activity

    0 讨论(0)
提交回复
热议问题