I have an ArrayList of objects. ie ArrayList<ObjectName>
.
I want to pass this to a new Activity
. I tried to use putParcelableArrayList
but it has issues with the object. I removed the <ObjectName>
part from the creating of the variable and the method works but then I get eclipse complaining about unsafe stuff.
How do I pass this ArrayList<ObjectName>
to a new Activity
Thanks for your time
EDIT I tried this :
ArrayList<ObjectName> arraylist = new Arraylist<ObjectName>(); Bundle bundle = new Bundle(); bundle.putParcelableArrayList("arraylist", arraylist);
I get the following Error:
The method `putParcelableArrayList(String, ArrayList<? extends Parcelable>)` in the type `Bundle` is not applicable for the arguments `(String, ArrayList<ObjectName>)`
EDIT2 Object Example Code. Do I need to changed this for Parcelable
to work?
public class ObjectName { private int value1; private int value2; private int value3; public ObjectName (int pValue1, int pValue2, int Value3) { value1 = pValue1; value2 = pValue2; value3 = pValue3; } // Get Statements for each value below public int getValue1() { return value1; } // etc
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.
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.
Your object class should implement parcelable. The code below should get you started.
public class ObjectName implements Parcelable { // Your existing code public ObjectName(Parcel in) { super(); readFromParcel(in); } public static final Parcelable.Creator<ObjectName> CREATOR = new Parcelable.Creator<ObjectName>() { public ObjectName createFromParcel(Parcel in) { return new ObjectName(in); } public ObjectName[] newArray(int size) { return new ObjectName[size]; } }; public void readFromParcel(Parcel in) { Value1 = in.readInt(); Value2 = in.readInt(); Value3 = in.readInt(); } public int describeContents() { return 0; } public void writeToParcel(Parcel dest, int flags) { dest.writeInt(Value1); dest.writeInt(Value2); dest.writeInt(Value3); } }
To use the above do this:
In 'sending' activity use:
ArrayList<ObjectName> arraylist = new Arraylist<ObjectName>(); Bundle bundle = new Bundle(); bundle.putParcelableArrayList("arraylist", arraylist);
In 'receiving' activity use:
Bundle extras = getIntent().getExtras(); ArrayList<ObjectName> arraylist = extras.getParcelableArrayList("arraylist"); ObjectName object1 = arrayList[0];
and so on.
Very Easy way, try the following:
bundle.putSerializable("lstContact", (Serializable) lstObject); lstObj = (List<Contacts>) bundle.getSerializable("lstContact");
and also you will need to implement your Contact class from Serializable interface. ex :
public class Contact implements Serializable{ ... ... ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With