54

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.

laalto
  • 137,703
  • 64
  • 254
  • 280
DCoder
  • 3,304
  • 7
  • 32
  • 64

4 Answers4

95

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");
Gowthaman M
  • 6,996
  • 7
  • 27
  • 51
Ravind Maurya
  • 2,489
  • 18
  • 27
  • 12
    Don't use `Serializable`, use `Parcelable` instead! [This is why](http://www.developerphil.com/parcelable-vs-serializable/). – gunar Jan 21 '14 at 06:09
  • 1
    Depends how are you implemented your code – Ravind Maurya Jan 21 '14 at 06:11
  • 1
    And what are those "Depends"? Can we know sir... ? – Pankaj Kumar Jan 21 '14 at 06:45
  • @PankajKumar `Parcelable` and `Serializable` are both interfaces that can be implemented using the `implements` keyword. Depends which you use in your class – Cbas Sep 09 '16 at 20:19
  • @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 – Felix Aug 04 '17 at 02:49
  • In second activity how to i set a value in textview @RavindMaurya – Ali May 18 '18 at 09:29
  • 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. – Abandoned Cart May 03 '19 at 12:43
28

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");`
Gowthaman M
  • 6,996
  • 7
  • 27
  • 51
chinnuu
  • 319
  • 2
  • 3
14

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");
Shayan Pourvatan
  • 11,622
  • 4
  • 39
  • 62
  • I am sure you meant: `ArrayList myList` instead of `ArrayList myList` – gunar Jan 21 '14 at 06:10
  • yes you right @gunar thanks for help – Shayan Pourvatan Jan 21 '14 at 06:12
  • 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. is this possible if the class is parcelable. – DCoder Jan 21 '14 at 06:43
  • 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 – Shayan Pourvatan Jan 21 '14 at 06:46
  • ok thanks. Will definitely try it out. – DCoder Jan 21 '14 at 06:49
  • @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 Jan 21 '14 at 07:13
  • @Dev i need see your code, because this code working fine, if you can't fix problem answer new question and notify me – Shayan Pourvatan Apr 12 '16 at 04:51
  • in second activity how to i set value in textview ? @ShayanPourvatan – Ali May 18 '18 at 09:28
  • @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. – Shayan Pourvatan May 18 '18 at 10:43
  • ok i'll use this code **`ArrayList myList = new ArrayList(); Intent intent = new Intent(getApplication(), SubCategoryData.class); intent.putExtra("SubCategory",myList); startActivity(intent);`** but `myList` size is **0** @ShayanPourvatan – Ali May 18 '18 at 10:53
  • this is not related to this post, you can ask another question. – Shayan Pourvatan May 18 '18 at 11:38
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);
Gowthaman M
  • 6,996
  • 7
  • 27
  • 51
Anjali Tripathi
  • 1,447
  • 9
  • 27