在 Intent 的 putExtra 方法的帮助下,如何像过去一样将 Collections likeArrayList
等从一个Activity
传递到另一个?Strings
int
任何人都可以帮助我,因为我想将一个List<String>
从一个传递Activity
到另一个?
在 Intent 的 putExtra 方法的帮助下,如何像过去一样将 Collections likeArrayList
等从一个Activity
传递到另一个?Strings
int
任何人都可以帮助我,因为我想将一个List<String>
从一个传递Activity
到另一个?
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).
首先你需要创建一个 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");
}
用于putExtra
将值传递给意图。使用getSerializableExtra
方法来检索这样的数据
活动一:
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 更好