How to pass ArrayList<CustomeObject> from one activity to another?

132,801

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);
Share:
132,801
DCoder
Author by

DCoder

Updated on July 09, 2022

Comments

  • DCoder
    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
    gunar over 10 years
    Don't use Serializable, use Parcelable instead! This is why.
  • gunar
    gunar over 10 years
    I am sure you meant: ArrayList<ContactClass> myList instead of ArrayList<String> myList
  • Ravind Maurya
    Ravind Maurya over 10 years
    Depends how are you implemented your code
  • Shayan Pourvatan
    Shayan Pourvatan over 10 years
    yes you right @gunar thanks for help
  • DCoder
    DCoder over 10 years
    I 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
    Pankaj Kumar over 10 years
    And what are those "Depends"? Can we know sir... ?
  • Shayan Pourvatan
    Shayan Pourvatan over 10 years
    sorry, 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
    DCoder over 10 years
    ok thanks. Will definitely try it out.
  • Shayan Pourvatan
    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
    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
    Cbas over 7 years
    @PankajKumar Parcelable and Serializable are both interfaces that can be implemented using the implements keyword. Depends which you use in your class
  • Felix
    Felix almost 7 years
    @Cbas I implement Parcelable . It returned null when i try get extra, although mMap has data. When i use Serializable it worked, im trying find out why
  • Ali
    Ali about 6 years
    how to i set value in textView ??
  • Ali
    Ali about 6 years
    in second activity how to i set value in textview ? @ShayanPourvatan
  • Ali
    Ali about 6 years
    In second activity how to i set a value in textview @RavindMaurya
  • Shayan Pourvatan
    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
    Ali about 6 years
    ok i'll use this code ArrayList<Pojo> myList = new ArrayList<Pojo>(); Intent intent = new Intent(getApplication(), SubCategoryData.class); intent.putExtra("SubCategory",myList); startActivity(intent); but myList size is 0 @ShayanPourvatan
  • Shayan Pourvatan
    Shayan Pourvatan about 6 years
    this is not related to this post, you can ask another question.
  • Anjali Tripathi
    Anjali Tripathi almost 6 years
    which value????
  • Ali
    Ali almost 6 years
    aaraylist in next activity
  • Anjali Tripathi
    Anjali Tripathi almost 6 years
    are u wanna set whole arraylist data to textview ?
  • Ali
    Ali almost 6 years
    no some fields..
  • Kartik Garasia
    Kartik Garasia over 5 years
    it does not seem to working , my app is keep crashing
  • Abandoned Cart
    Abandoned Cart about 5 years
    The 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.