Android 将列表从一个活动传递到另一个活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11340776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing a List from one Activity to another
提问by Name is Nilay
How to pass Collections like ArrayList
, etc from one Activity
to another as we used to pass Strings
, int
with help of putExtra method of Intent?
在 Intent 的 putExtra 方法的帮助下,如何像过去一样将诸如ArrayList
, etc 之类的集合从一个Activity
传递到另一个?Strings
int
Can anyone please help me as i want to pass a List<String>
from one Activity
to another?
任何人都可以帮助我,因为我想List<String>
从一个传递Activity
到另一个?
回答by nhaarman
You can pass an ArrayList<E>
the same way, if the E
type is Serializable
.
你可以传递一个ArrayList<E>
同样的方式,如果E
类型Serializable
。
You would call the putExtra (String name, Serializable value)
of Intent
to store, and getSerializableExtra (String name)
for retrieval.
您可以调用putExtra (String name, Serializable value)
ofIntent
来存储和getSerializableExtra (String name)
检索。
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).
请注意,序列化可能会导致性能问题:它需要时间,并且将分配大量对象(因此必须进行垃圾回收)。
回答by Shahin ShamS
First you need to create a Parcelable object class, see the example
首先需要创建一个 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];
}
};
}
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");
}
回答by niky_d
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...
我希望这会帮助某人...
回答by RajaReddy PolamReddy
use putExtra
to pass value to an intent. use getSerializableExtra
method to retrieve the data
like this
用于putExtra
将值传递给意图。使用getSerializableExtra
方法来检索这样的数据
Activity A :
活动一:
ArrayList<String> list = new ArrayList<String>();
intent.putExtra("arraylist", list);
startActivity(intent);
Activity B:
活动乙:
ArrayList<String> list = getIntent().getSerializableExtra("arraylist");
回答by AkashG
To pass ArrayList from one activity to another activity, one should include
要将 ArrayList 从一个活动传递到另一个活动,应该包括
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
在开始活动之前。并在另一个活动中获取 ArrayList 包括
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.
您将能够通过此传递 ArrayList。希望这对您有所帮助。