How to pass ArrayList<CustomeObject> from one activity to another?
Solution 1
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");
Solution 2
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");`
Solution 3
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");
Solution 4
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);
DCoder
Updated on July 09, 2022Comments
-
DCoder almost 2 years
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.
-
gunar over 10 years
-
gunar over 10 yearsI am sure you meant:
ArrayList<ContactClass> myList
instead ofArrayList<String> myList
-
Ravind Maurya over 10 yearsDepends how are you implemented your code
-
Shayan Pourvatan over 10 yearsyes you right @gunar thanks for help
-
DCoder over 10 yearsI have never used Parcelable. but as I have come to know Parcelable is 10 times faster than serializable, will look forward to use parcelable. I have getter setter methods in my ContactBean class which I simply use to store data in class variables after making its object and then I am adding that object to ArrayList<ContactBean>. is this possible if the class is parcelable.
-
Pankaj Kumar over 10 yearsAnd what are those "Depends"? Can we know sir... ?
-
Shayan Pourvatan over 10 yearssorry, i don't get your mean, but if i want tell you simple, you need both above method and getter and setter method.i edit my class
-
DCoder over 10 yearsok thanks. Will definitely try it out.
-
Shayan Pourvatan over 10 years@VarunKarhadkar yes is possible , if is not possible then why we use that? sorry i'm busy first time don't get your mean.
-
Shayan Pourvatan about 8 years@Dev i need see your code, because this code working fine, if you can't fix problem answer new question and notify me
-
Cbas over 7 years@PankajKumar
Parcelable
andSerializable
are both interfaces that can be implemented using theimplements
keyword. Depends which you use in your class -
Felix almost 7 years@Cbas I implement
Parcelable
. It returned null when i try get extra, althoughmMap
has data. When i useSerializable
it worked, im trying find out why -
Ali about 6 yearshow to i set value in textView ??
-
Ali about 6 yearsin second activity how to i set value in textview ? @ShayanPourvatan
-
Ali about 6 yearsIn second activity how to i set a value in textview @RavindMaurya
-
Shayan Pourvatan about 6 years@MohammadAli what is your mean? set it as normally, you've got your object, now you can set it with
setText
method with your property that you need. -
Ali about 6 yearsok i'll use this code
ArrayList<Pojo> myList = new ArrayList<Pojo>(); Intent intent = new Intent(getApplication(), SubCategoryData.class); intent.putExtra("SubCategory",myList); startActivity(intent);
butmyList
size is 0 @ShayanPourvatan -
Shayan Pourvatan about 6 yearsthis is not related to this post, you can ask another question.
-
Anjali Tripathi almost 6 yearswhich value????
-
Ali almost 6 years
aaraylist
in next activity -
Anjali Tripathi almost 6 yearsare u wanna set whole arraylist data to textview ?
-
Ali almost 6 yearsno some fields..
-
Kartik Garasia over 5 yearsit does not seem to working , my app is keep crashing
-
Abandoned Cart about 5 yearsThe why that @gunar is referencing is that it
Parcelable
is about 10 times faster, while still using less resources. The downside is that the data types are limited (which the article overlooks) and you have to implement it yourself.