19

在 Intent 的 putExtra 方法的帮助下,如何像过去一样将 Collections likeArrayList等从一个Activity传递到另一个?Stringsint

任何人都可以帮助我,因为我想将一个List<String>从一个传递Activity到另一个?

4

6 回答 6

45

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).

于 2012-07-05T08:49:57.797 回答
23

首先你需要创建一个 Parcelable 对象类,看例子

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];
            }
        };
    }

和名单

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

调用活动的代码

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

调用活动的代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);   

    Bundle bundle = getIntent().getExtras();
    ArrayList<Student> arraylist = bundle.getParcelableArrayList("mylist");
}
于 2015-01-27T15:30:00.000 回答
5

用于putExtra将值传递给意图。使用getSerializableExtra方法来检索这样的数据

活动一:

ArrayList<String> list = new ArrayList<String>();

intent.putExtra("arraylist", list);
startActivity(intent);

活动 B:

ArrayList<String> list = getIntent().getSerializableExtra("arraylist");
于 2012-07-05T08:51:34.793 回答
5

我尝试了所有提出的技术,但都没有奏效并阻止我的应用程序工作,然后我终于成功了。我是这样做的……在主要活动中,我这样做了:

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

要获取新 Activity 中的数据:

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

我希望这会对某人有所帮助...

于 2013-06-01T13:10:39.970 回答
1

要将 ArrayList 从一个活动传递到另一个活动,应该包括

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

在开始活动之前。并在另一个活动中获取 ArrayList 包括

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

您将能够通过此传递 ArrayList。希望这对您有所帮助。

于 2012-07-05T09:10:04.393 回答
0

使用 ViewModel 比 Serializable 或 Parcelable 更好

于 2021-08-01T16:10:13.570 回答