Android 将对象的ArrayList传递给新活动
我有一个对象的数组列表。ieAndroid 将对象的ArrayList传递给新活动,android,Android,我有一个对象的数组列表。ieArrayList 我想将其传递给新的活动。我试图使用putParcelableArrayList,但它与对象有问题。我从创建变量的过程中删除了部分,该方法可以工作,但后来我发现eclipse抱怨不安全的东西 如何将此ArrayList传递给新活动 谢谢你的时间 编辑 我试过这个: ArrayList<ObjectName> arraylist = new Arraylist<ObjectName>(); Bundle bundle = new
ArrayList
我想将其传递给新的活动
。我试图使用putParcelableArrayList
,但它与对象有问题。我从创建变量的过程中删除了
部分,该方法可以工作,但后来我发现eclipse抱怨不安全的东西
如何将此ArrayList
传递给新活动
谢谢你的时间
编辑
我试过这个:
ArrayList<ObjectName> arraylist = new Arraylist<ObjectName>();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("arraylist", arraylist);
您可以在某些类中使用
公共静态
字段
请参阅以获取更多建议。当我不得不这样做时,我只需将对象的ArrayList放入extras捆绑包(它必须是ArrayList,列表显然不可序列化)。然后,您所要做的就是确保列表中的对象也是可序列化的。您的对象类应该实现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);
}
}
public类ObjectName实现了Parcelable{
//您现有的代码
公共对象名称(地块中){
超级();
从包裹中读取;
}
public static final Parcelable.Creator=新建Parcelable.Creator(){
public ObjectName createFromParcel(中的地块){
返回新的ObjectName(in);
}
public ObjectName[]新数组(整数大小){
返回新的ObjectName[size];
}
};
公共无效readFromParcel(地块位于中){
Value1=in.readInt();
Value2=in.readInt();
Value3=in.readInt();
}
公共int描述内容(){
返回0;
}
公共无效写入包裹(包裹目的地,内部标志){
目的地记录(值1);
目的地记录(价值2);
目的地记录(价值3);
}
}
要使用上述方法,请执行以下操作:
在“发送”活动中使用:
ArrayList<ObjectName> arraylist = new Arraylist<ObjectName>();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("arraylist", arraylist);
ArrayList ArrayList=new ArrayList();
Bundle=新Bundle();
bundle.putParcelableArrayList(“arraylist”,arraylist);
在“接收”活动中使用:
Bundle extras = getIntent().getExtras();
ArrayList<ObjectName> arraylist = extras.getParcelableArrayList("arraylist");
ObjectName object1 = arrayList[0];
Bundle extras=getIntent().getExtras();
ArrayList ArrayList=extras.getParcelableArrayList(“ArrayList”);
ObjectName object1=arrayList[0];
依此类推。您有两个选择:
- ArrayList中的对象按方法的要求实现
- 或者对象可以实现并使用该方法添加ArrayList,即
bundle.putSerializable(“ArrayList”,ArrayList)代码>
大多数对象可以通过
Serializable
轻松序列化,实现Serializable
并向类中添加静态UID比编写Parcelable
所需的所有方法/类要简单得多。查看putParcelableArrayList
的文档,它看起来像您的ObjectName
需要扩展“Parcelable”接口才能正常工作。您得到的错误似乎也表明了这一点
我想看看这里:非常简单的方法,尝试以下方法:
bundle.putSerializable("lstContact", (Serializable) lstObject);
lstObj = (List<Contacts>) bundle.getSerializable("lstContact");
我也遇到了同样的问题,并找到了一种更好的方法,将我的数组放入全局类,可以跨所有活动访问。感谢post介绍如何使用可跨多个活动访问的全局变量 我的环球班是
公共类GlobalClass扩展应用程序{
公共MyObject[]对象
}
Android清单更改
android:name=".GlobalClass"
在多个活动中访问,如
GlobalClass g = (GlobalClass)getApplicationContext();
g.objArray[0].x = 100;
int x = g.objArray[0].x;
为了更好地理解,请阅读本文。您可以使用可序列化接口传递您的arraylist。(序列化是将对象转换为字节流以存储该对象或将其传输到内存、数据库或文件的过程。) 现在在您当前的活动中
// 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);
//这是您的arrayList
ArrayList objNamesList=新的ArrayList();
add(新的ObjectName(1,1,1));
add(新的ObjectName(2,2,2));
//初始化包实例
Bundle b=新Bundle();
//将数据添加到捆绑包中
//key=>arrayList的objNames引用
//value=>objNamesList,arrayList的名称
//将arraylist强制转换为可序列化
b、 putSerializable(“objNames”,(Serializable)objNamesList);
//初衷
意图i=新意图(CurrentActivity.this、NextActivity.class);
i、 putExtras(b);
星触觉(i);
在下一个活动中,您需要执行以下操作以获取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");
}
}
public class nextractivity扩展了AppCompativity活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u next\u activity);
//拿到包裹
Bundle b=getIntent().getExtras();
ArrayList q=(ArrayList)b.getSerializable(“对象”);
}
}
1)确保要打包的对象已正确实现
2) 在:new ArrayList(列表)中使用此参数
示例:
protected void onSaveInstanceState(Bundle outstate) {
super.onSaveInstanceState(outstate);
outstate.putParcelableArrayList("myObjects", new ArrayList<>(myListOfObjects));
}
SaveInstanceState上受保护的无效(束超出状态){
super.onSaveInstanceState(超出状态);
outstate.putParcelableArrayList(“myObjects”,新ArrayList(myListOfObjects));
}
帮助我的是:
写入地块时设置所有值。若任何值为NULL,我将为该特定属性设置deafult值。
为几个参数设置了空值,这就是引发此错误的原因。您能描述一下这些对象是什么吗?一些相关的代码会非常有用。你的包裹怎么了?添加代码,因为parcelable是您想要使用的方式。每个对象都是以连续方式创建的
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
}
// 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);
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");
}
}
protected void onSaveInstanceState(Bundle outstate) {
super.onSaveInstanceState(outstate);
outstate.putParcelableArrayList("myObjects", new ArrayList<>(myListOfObjects));
}