问题
I have a class like:
Class persona implements Serializable {
int age;
String name;
}
And my first Activity fill an array:
persona[] p;
Then, I need this info in another Activity. How I can send it?
I try to make:
Bundle b = new Bundle();
b.putSerializable("persona", p);
But I Can't.
回答1:
AFAIK the is no method that put a serializable array into bundle any way here is a solution to use that uses parcel
change you class to this
import android.os.Parcel;
import android.os.Parcelable;
public class persona implements Parcelable {
int age;
String name;
public static final Parcelable.Creator<persona> CREATOR = new Creator<persona>() {
@Override
public persona[] newArray(int size) {
// TODO Auto-generated method stub
return new persona[size];
}
@Override
public persona createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new persona(source);
}
};
public persona(Parcel in) {
super();
age = in.readInt();
name = in.readString();
}
public persona() {
super();
// TODO Auto-generated constructor stub
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(age);
dest.writeString(name);
}
}
then you can send the array like this
Bundle b = new Bundle();
b.putParcelableArray("persona", p);
btw using Parcelable instead of Serializable is more efficient in Android
回答2:
You class will need to implement Parcelable. Then, you can send it in a bundle by using the Bundle.putParcelableArray()-method.
Also, a general advice: Class names should always start uppercase
来源:https://stackoverflow.com/questions/13778485/how-do-i-send-an-array-of-objects-from-one-activity-to-another