Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 794
DCoder Avatar asked Jan 21 '14 06:01

DCoder


People also ask

How pass ArrayList from activity to fragment in Android?

If you want to pass an ArrayList to your fragment, then you need to make sure the Model class is implements Parcelable. Here i can show an example. then you can add ArrayList<ObjectName> to a Bundle object. ArrayList<ObjectName> arraylist = new Arraylist<ObjectName>(); Bundle bundle = new Bundle(); bundle.


4 Answers

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");
like image 69
Ravind Maurya Avatar answered Oct 04 '22 14:10

Ravind Maurya


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");`
like image 27
chinnuu Avatar answered Oct 04 '22 13:10

chinnuu


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");
like image 22
Shayan Pourvatan Avatar answered Oct 04 '22 14:10

Shayan Pourvatan


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);
like image 40
Anjali Tripathi Avatar answered Oct 04 '22 13:10

Anjali Tripathi