帮助过的对象一个ArrayList到一个新的活动 [英] Help passing an ArrayList of Objects to a new Activity
问题描述
我有对象的ArrayList。即ArrayList中。
我想通过这一个新的活动。我试图用putParcelableArrayList,但它具有与对象的问题。我删除了部分自变量的创建和方法的工作,但后来我得到eciplse抱怨不安全的东西。
我如何通过此ArrayList到一个新的活动
感谢您的时间
修改 我想这样的:
的ArrayList<的ObjectName> ArrayList的=新的ArrayList<的ObjectName>();
束束=新包();
bundle.putParcelableArrayList(数组列表,ArrayList的);
我收到以下错误:
的方法putParcelableArrayList(字符串,ArrayList的&LT ;?延伸Parcelable>)的类型包不适用的参数(字符串,ArrayList的<的ObjectName>)
EDIT2 对象的实例code。我需要改变本作paracbale工作?
公共类的ObjectName {
私人诠释值1;
私人诠释值2;
私人诠释值3;
公众的ObjectName(INT pValue1,诠释pValue2,INT值3){
值1 = pValue1;
值2 = pValue2;
值3 = pValue3;
}
//获取报表下面每个值
公众诠释getValue1(){
返回值1;
}
// 等等
您的对象类应该实现parcelable。在code以下应该让你开始。
公共类的ObjectName实现Parcelable {
//您现有的code
公众的ObjectName(包裹中){
超();
readFromParcel(在);
}
公共静态最终Parcelable.Creator<的ObjectName> CREATOR =新Parcelable.Creator<的ObjectName>(){
公众的ObjectName createFromParcel(包裹中){
返回新的ObjectName(中);
}
公众的ObjectName [] newArray(INT尺寸){
返回新的ObjectName【尺寸】;
}
};
公共无效readFromParcel(包裹中){
值1 = in.readInt();
值2 = in.readInt();
值3 = in.readInt();
}
公众诠释describeContents(){
返回0;
}
公共无效writeToParcel(包裹DEST,INT标志){
dest.writeInt(值1);
dest.writeInt(值2);
dest.writeInt(值3);
}
}
要使用上述做到这一点:
在'送'活动中使用:
的ArrayList<的ObjectName> ArrayList的=新的ArrayList<的ObjectName>();
束束=新包();
bundle.putParcelableArrayList(数组列表,ArrayList的);
在'接收'活动中使用:
捆绑额外= getIntent()getExtras()。
ArrayList的<的ObjectName> ArrayList的= extras.getParcelableArrayList(数组列表);
的ObjectName object1 = ArrayList的[0];
等等。
I have an arraylist of objects. ie ArrayList.
I want to pass this to a new Activity. I tried to use putParcelableArrayList but it has issues with the object. I removed the part from the creating of the variable and the method works but then I get eciplse complaining about unsafe stuff.
How do I pass this arrayList 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 paracbale 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
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.
这篇关于帮助过的对象一个ArrayList到一个新的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!