Passing a List from one Activity to another

后端 未结 5 2122
花落未央
花落未央 2020-12-01 13:09

How to pass Collections like ArrayList, etc from one Activity to another as we used to pass Strings, int with help of put

相关标签:
5条回答
  • 2020-12-01 13:27

    To pass ArrayList from one activity to another activity, one should include

    intent.putStringArrayListExtra(KEY, list); //where list is ArrayList which you want to pass
    

    before starting the activity.And to get the ArrayList in another activity include

    Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
                temp = bundle.getStringArrayList(KEY); // declare temp as ArrayList
                    }
    

    you will be able to pass ArrayList through this.Hope this will be helpful to you.

    0 讨论(0)
  • 2020-12-01 13:33

    You can pass an ArrayList<E> the same way, if the E type is Serializable.

    You would call the putExtra (String name, Serializable value) of Intent to store, and getSerializableExtra (String name) for retrieval.

    Example:

    ArrayList<String> myList = new ArrayList<String>();
    intent.putExtra("mylist", myList);
    

    In the other Activity:

    ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist");
    

    Please note that serialization can cause performance issues: it takes time, and a lot of objects will be allocated (and thus, have to be garbage collected).

    0 讨论(0)
  • 2020-12-01 13:42

    First you need to create a Parcelable object class, see the example

    public class Student implements Parcelable {
    
            int id;
            String name;
    
            public Student(int id, String name) {
                this.id = id;
                this.name = name;
    
            }
    
            public int getId() {
                return id;
            }
    
            public String getName() {
                return name;
            }
    
    
            @Override
            public int describeContents() {
                // TODO Auto-generated method stub
                return 0;
            }
    
            @Override
            public void writeToParcel(Parcel dest, int arg1) {
                // TODO Auto-generated method stub
                dest.writeInt(id);
                dest.writeString(name);
            }
    
            public Student(Parcel in) {
                id = in.readInt();
                name = in.readString();
            }
    
            public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {
                public Student createFromParcel(Parcel in) {
                    return new Student(in);
                }
    
                public Student[] newArray(int size) {
                    return new Student[size];
                }
            };
        }
    

    And the list

    ArrayList<Student> arraylist = new ArrayList<Student>();
    

    Code from Calling activity

    Intent intent = new Intent(this, SecondActivity.class);
    Bundle bundle = new Bundle();
    bundle.putParcelableArrayList("mylist", arraylist);
    intent.putExtras(bundle);       
    this.startActivity(intent);
    

    Code on called activity

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);   
    
        Bundle bundle = getIntent().getExtras();
        ArrayList<Student> arraylist = bundle.getParcelableArrayList("mylist");
    }
    
    0 讨论(0)
  • 2020-12-01 13:43

    use putExtra to pass value to an intent. use getSerializableExtra method to retrieve the data like this

    Activity A :

    ArrayList<String> list = new ArrayList<String>();
    
    intent.putExtra("arraylist", list);
    startActivity(intent);
    

    Activity B:

    ArrayList<String> list = getIntent().getSerializableExtra("arraylist");
    
    0 讨论(0)
  • 2020-12-01 13:49

    I tried all the proposed techniques but none of them worked and stopped my app from working and then I finally succedded. Here is how I did it... In main activity I did this:

                List<String> myList...;
                Intent intent = new Intent...;
                Bundle b=new Bundle();
                b.putStringArrayList("KEY",(ArrayList<String>)myList);            
                intent_deviceList.putExtras(b);
                ....startActivity(intent);
    

    To get the data in the new Activity:

        List<String> myList...
        Bundle b = getIntent().getExtras();
        if (b != null) {
            myList = bundle.getStringArrayList("KEY");
        }
    

    I hope this will help someone...

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