【发布时间】:2012-07-05 15:32:45
【问题描述】:
如何通过 Intent 的 putExtra 方法将 ArrayList 等集合从一个 Activity 传递到另一个集合?
谁能帮助我,因为我想将List<String> 从一个Activity 传递给另一个?
【问题讨论】:
标签: android listview android-intent arraylist
如何通过 Intent 的 putExtra 方法将 ArrayList 等集合从一个 Activity 传递到另一个集合?
谁能帮助我,因为我想将List<String> 从一个Activity 传递给另一个?
【问题讨论】:
标签: android listview android-intent arraylist
如果E 类型是Serializable,您可以以同样的方式传递ArrayList<E>。
您可以调用Intent 的putExtra (String name, Serializable value) 进行存储,调用getSerializableExtra (String name) 进行检索。
例子:
ArrayList<String> myList = new ArrayList<String>();
intent.putExtra("mylist", myList);
在另一个活动中:
ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist");
请注意,序列化可能会导致性能问题:它需要时间,并且会分配大量对象(因此必须进行垃圾回收)。
【讨论】:
List 接口没有扩展Serializable,因此如果您的集合是由接口声明的,则必须先将其转换为特定的实现。
首先你需要创建一个 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");
}
【讨论】:
writeToParcel 中给出。是否有任何机制可以根据密钥从Parcel 对象获取值?例如,如果在课堂上我们有多个字符串字段,那么我们如何才能知道我们从 Student(Parcel in) 中的 Parcel 得到哪个字符串。
使用putExtra 将值传递给意图。使用getSerializableExtra 方法检索数据
像这样
活动 A:
ArrayList<String> list = new ArrayList<String>();
intent.putExtra("arraylist", list);
startActivity(intent);
活动 B:
ArrayList<String> list = getIntent().getSerializableExtra("arraylist");
【讨论】:
我尝试了所有建议的技术,但都没有奏效并阻止我的应用程序运行,然后我终于成功了。这就是我是如何做到的...... 在主要活动中,我这样做了:
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");
}
我希望这会对某人有所帮助...
【讨论】:
要将 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。希望这对您有所帮助。
【讨论】:
使用 ViewModel 比 Serializable 或 Parcelable 更有效
【讨论】: