Android 如何将 ArrayList<CustomeObject> 从一个活动传递到另一个活动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21250339/
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
How to pass ArrayList<CustomeObject> from one activity to another?
提问by DCoder
I want to send Following ArrayList from one activity to another please help.
我想将以下 ArrayList 从一个活动发送到另一个活动,请帮忙。
ContactBean m_objUserDetails = new ContactBean();
ArrayList<ContactBean> ContactLis = new ArrayList<ContactBean>();
I am sending the above arraylist after adding data in it as follows
我在添加数据后发送上面的arraylist,如下所示
Intent i = new Intent(this,DisplayContact.class);
i.putExtra("Contact_list", ContactLis);
startActivity(i);
But I am getting problem while recovering it.
但是我在恢复它时遇到了问题。
ArrayList<ContactBean> l1 = new ArrayList<ContactBean>();
Bundle wrapedReceivedList = getIntent().getExtras();
l1= wrapedReceivedList.getCharSequenceArrayList("Contact_list");
At this point I am getting this error:
此时我收到此错误:
Type mismatch: cannot convert from ArrayList<CharSequence> to ArrayList<ContactBean>
My ContactBean class implements Serializable please also tell why we have to implement serializable interface.
我的 ContactBean 类实现了 Serializable 请告诉我们为什么我们必须实现可序列化的接口。
回答by Ravind Maurya
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");
回答by chinnuu
In First activity:
在第一个活动中:
ArrayList<ContactBean> fileList = new ArrayList<ContactBean>();
Intent intent = new Intent(MainActivity.this, secondActivity.class);
intent.putExtra("FILES_TO_SEND", fileList);
startActivity(intent);
In receiver activity:
在接收器活动中:
ArrayList<ContactBean> filelist = (ArrayList<ContactBean>)getIntent().getSerializableExtra("FILES_TO_SEND");`
回答by Shayan Pourvatan
you need implements Parcelablein your ContactBean
class, I put one example for you:
你需要在你的班级中实现 ParcelableContactBean
,我为你举了一个例子:
public class ContactClass implements Parcelable {
private String id;
private String photo;
private String firstname;
private String lastname;
public ContactClass()
{
}
private ContactClass(Parcel in) {
firstname = in.readString();
lastname = in.readString();
photo = in.readString();
id = in.readString();
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(firstname);
dest.writeString(lastname);
dest.writeString(photo);
dest.writeString(id);
}
public static final Parcelable.Creator<ContactClass> CREATOR = new Parcelable.Creator<ContactClass>() {
public ContactClass createFromParcel(Parcel in) {
return new ContactClass(in);
}
public ContactClass[] newArray(int size) {
return new ContactClass[size];
}
};
// all get , set method
}
and this get and set for your code:
这为您的代码获取和设置:
Intent intent = new Intent(this,DisplayContact.class);
intent.putExtra("Contact_list", ContactLis);
startActivity(intent);
second class:
第二类:
ArrayList<ContactClass> myList = getIntent().getParcelableExtra("Contact_list");
回答by Anjali Tripathi
Use this code to pass arraylist<customobj>
to anthother Activity
使用此代码传递arraylist<customobj>
给另一个Activity
firstly serialize our contact bean
首先序列化我们的联系bean
public class ContactBean implements Serializable {
//do intialization here
}
Now pass your arraylist
现在传递你的数组列表
Intent intent = new Intent(this,name of activity.class);
contactBean=(ConactBean)_arraylist.get(position);
intent.putExtra("contactBeanObj",conactBean);
_activity.startActivity(intent);