我设置了一个名为“Contact”的类,Contact类包含Getters和Setters。 在主要活动中。
List<Contact> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private class GetContacts extends AsyncTask<String, String, List<Contact>>{
@Override
protected void onPreExecute() {
//
}
@Override
protected List<Contacts> doInBackground(String... params) {
// executing some tasks;
return contactList;
}
@Override
protected void onPostExecute(List<Contact> result) {
contactList = result;
}
现在我想将contactList发送给另一个活动。
答案 0 :(得分:1)
如果ArrayList<T>
类型为T
,您可以采用相同的方式传递Serializable
。
您可以调用putExtra (String name, Serializable value)
Intent
来存储,getSerializableExtra (String name)
进行检索。
示例:
ArrayList<String> your_list= new ArrayList<String>();
intent.putExtra("your_list", your_list);
在另一项活动中:
ArrayList<String> your_list = (ArrayList<String>) getIntent().getSerializableExtra("your_list");
答案 1 :(得分:1)
putParcelableArrayListExtra(String keyValue, ArrayList< ? extends Parcelable);
使用Intent类中的上述方法。 注意:自定义类(?)必须实现Parcelable接口类。
答案 2 :(得分:1)
您的Contact.java类
public class GymItem implements Serializable{
}
MainActivity.class
Intent intent = new Intent(MainActivity.this,
OtherActivity.class);
Bundle bundleObject = new Bundle();
bundleObject.putSerializable("KEY", contactList);
intent.putExtras(bundleObject);
startActivity(intent);
OtherActivity.java
ArrayList<Contact> contactList = new ArrayList<Contact>();
Bundle bundleObject = getIntent().getExtras();
contactList = (ArrayList<Contact>) bundleObject
.getSerializable("KEY");
答案 3 :(得分:1)
A类扩展了活动{
List<Contact> contactList; //your contact list Intent AtoB = new Intent(getApplicationContext(),B.class); AtoB.putParcelableArrayListExtra("Contacts",contactList); startActivity(AtoB);
}
class Contact implements Parcelable{ private String name; private String phoneNo; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeString(phoneNo); } public Contact(){ } private Contact(Parcel in) { name = in.readString(); phoneNo = in.readString(); } public static final Parcelable.Creator<Contact> CREATOR = new Parcelable.Creator<Contact>() { public Contact[] newArray(int size) { return new Contact[size]; } public ContactcreateFromParcel(Parcel in) { // TODO Auto-generated method stub return new Contact(in); } }; } class B extends Activity{ ArrayList<Contact> listfromA = getIntent().getParcelableExtra("Contacts"); }