How to pass ArrayList<CustomeObject> from one activity to another? [duplicate]
Asked Answered
A

3

55

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.

Anzac answered 21/1, 2014 at 6:2 Comment(3)
#15790999Marci
make your custom Object Parcelable.Frugivorous
Accepted answer copied from stackoverflow.com/a/11340842Genuflect
P
31

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");`
Propraetor answered 21/1, 2014 at 6:10 Comment(1)
it does not seem to working , my app is keep crashingSassoon
C
15

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");
Cascara answered 21/1, 2014 at 6:8 Comment(11)
I am sure you meant: ArrayList<ContactClass> myList instead of ArrayList<String> myListHochstetler
yes you right @Hochstetler thanks for helpCascara
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.Anzac
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 classCascara
ok thanks. Will definitely try it out.Anzac
@VarunKarhadkar yes is possible , if is not possible then why we use that? sorry i'm busy first time don't get your mean.Cascara
@Dev i need see your code, because this code working fine, if you can't fix problem answer new question and notify meCascara
in second activity how to i set value in textview ? @ShayanPourvatanChemo
@Chemo 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.Cascara
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 @ShayanPourvatanChemo
this is not related to this post, you can ask another question.Cascara
M
5

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);
Marcela answered 21/1, 2014 at 6:38 Comment(5)
how to i set value in textView ??Chemo
which value????Marcela
aaraylist in next activityChemo
are u wanna set whole arraylist data to textview ?Marcela
no some fields..Chemo

© 2022 - 2024 — McMap. All rights reserved.