Passing an ArrayList of Objects to the new Activity
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 = pVal开发者_开发百科ue1;
value2 = pValue2;
value3 = pValue3;
}
// Get Statements for each value below
public int getValue1() {
return value1;
}
// etc
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{
...
...
...
}
You have two options:
Objects in the ArrayList put implement
Parceable
as required by the methodputParcelableArrayList ()
Or the objects can implement
Serializable
and use the methodputSerializable()
to add the ArrayList, iebundle.putSerializable("arraylist", arraylist);
Android passes Object in Bundle by serializing and deserializing (via Serializable or Parcelable). So all of your Objects you want to pass in a Bundle must implement one of those two interfaces.
Most Object serialize easily with Serializable
and it is much simpler to implement Serializable
and add a static UID to the class than to write all the methods/classes required for Parcelable
.
You can pass your arraylist using Serializable interface. (Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file.)
public class ObjectName implements Serializable{
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
}
Now in your current activity
// here is the arrayList of yours
ArrayList<ObjectName> objNamesList = new ArrayList<>();
objNamesList.add(new ObjectName(1,1,1));
objNamesList.add(new ObjectName(2,2,2));
//intialize bundle instance
Bundle b = new Bundle();
//adding data into the bundle
//key => objNames referece for the arrayList
//value => objNamesList , name of the arrayList
//cast the arraylist into Serializable
b.putSerializable("objNames", (Serializable) objNamesList);
//init the intent
Intent i = new Intent(CurrentActivity.this, NextActivity.class);
i.putExtras(b);
startActivity(i);
at next activity you will need to do as follows to get the arraylist
public class NextActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next_activity);
// get the bundle
Bundle b = getIntent().getExtras();
ArrayList<ObjectName> q = (ArrayList<ObjectName>) b.getSerializable("objNames");
}
}
Looking at the documentation for putParcelableArrayList
it looks like your ObjectName
needs to extend the 'Parcelable' interface in order for this to work. The error you get seems to indicate this as well.
I would look here: http://developer.android.com/reference/android/os/Parcelable.html
When I had to do that I simply put the ArrayList of objects into the extras bundle (it has to be an ArrayList, List is not Serializable apparently). Then all you have to do then is ensure the objects inside the list are serializable also.
1) Make sure the object you want to parcel has correctly implemented Parcelable
2) Use this argument in putParcelableArrayList: new ArrayList<>(list)
Example:
protected void onSaveInstanceState(Bundle outstate) {
super.onSaveInstanceState(outstate);
outstate.putParcelableArrayList("myObjects", new ArrayList<>(myListOfObjects));
}
what helped me was: setting all values while writing to the parcel. In case any value is NULL, I am setting deafult value for that particular property. Null values were getting set for few parameters, and thats why this error was being thrown.
You can use a public static
field in some of your classes.
See here for more advices.
I was struggling with same issue and found a much better approach of putting my array in Global Class that can be accessed across all activities. Thanks to this post for describing how to use global variables that can be accessed across multiple activities.
My global class is
public class GlobalClass extends Application{
public MyObject[] objArray
}
Android Manifest Change
android:name=".GlobalClass"
Accessed in multiple activities as
GlobalClass g = (GlobalClass)getApplicationContext();
g.objArray[0].x = 100;
int x = g.objArray[0].x;
For Better understanding please go through the post.
精彩评论