How to pass ArrayList<CustomeObject> from one activity to another? [duplicate]
I want to send Following ArrayList from one activity to another please help.
ContactBean m_objUserDetails = new ContactBean();
ArrayList<ContactBean> ContactLis = new ArrayList<ContactBean>();
I am sending the above arraylist after adding data in it as follows
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.
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");
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");`
you need implements Parcelable in your ContactBean
class, I put one example for you:
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");
Use this code to pass arraylist<customobj>
to anthother Activity
firstly serialize our contact 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);