我有一个对象的 ArrayList。即ArrayList<ObjectName>
.
我想将此传递给新的 Activity
.我尝试使用 putParcelableArrayList
但它与对象有问题。我删除了 <ObjectName>
部分来自变量的创建和方法的工作,但后来我得到 eclipse 提示不安全的东西。
如何通过 ArrayList<ObjectName>
到一个新的 Activity
感谢您的宝贵时间
编辑 我试过这个:
ArrayList<ObjectName> arraylist = new Arraylist<ObjectName>();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("arraylist", arraylist);
我收到以下错误:
The method `putParcelableArrayList(String, ArrayList<? extends Parcelable>)` in the type `Bundle` is not applicable for the arguments `(String, ArrayList<ObjectName>)`
EDIT2
对象示例代码。我需要为 Parcelable
更改此设置吗?上类?
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
最佳答案
你的对象类应该实现 parcelable。下面的代码应该可以帮助您入门。
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);
}
}
要使用上述内容,请执行以下操作:
在“发送” Activity 中使用:
ArrayList<ObjectName> arraylist = new Arraylist<ObjectName>();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("arraylist", arraylist);
在“接收” Activity 中使用:
Bundle extras = getIntent().getExtras();
ArrayList<ObjectName> arraylist = extras.getParcelableArrayList("arraylist");
ObjectName object1 = arrayList[0];
等等。
关于android - 将对象的 ArrayList 传递给新的 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6681217/