[英]Pass Arraylist of objects in another activity
I am trying to send an array list of objects in another activity. 我正在尝试在另一个活动中发送对象的数组列表。 I have checked many articles on this topic and i am using parcelable but i am stuck at point where i cannot send the object.I have tried many things.This is the thing i am trying. 我已经检查了很多关于该主题的文章,并且我使用的是包裹式包裹,但是我无法发送对象。我尝试了很多事情。这就是我正在尝试的事情。
public class ParcalableForm implements Parcelable {
private ArrayList<form> from;
public ParcalableForm (ArrayList<form> choices) {
this.from = choices;
}
public ParcalableForm (Parcel parcel) {
this.from = parcel.readArrayList(null);
}
@Override
public int describeContents() {
return 0;
}
// Required method to write to Parcel
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(from);
}
// Method to recreate a Question from a Parcel
public static Creator<ParcalableForm> CREATOR = new Creator<ParcalableForm>() {
@Override
public ParcalableForm createFromParcel(Parcel source) {
return new ParcalableForm(source);
}
@Override
public ParcalableForm[] newArray(int size) {
return new ParcalableForm[size];
}
};
} }
This is the parcelable class that implements Parcelable.I am trying to send Arraylist of form to another activity. 这是实现Parcelable的parcelable类。我正在尝试将表单的Arraylist发送到另一个活动。
Intent i = new Intent(UserPage.this,Form.class);
Bundle extras = new Bundle();
System.out.println("I found a form :- ");
ParcalableForm p=new ParcalableForm(f1.attr);
i.putExtra("geopoints", p);
startActivity(i);
This is the class which is sending the object to the other activity. 这是将对象发送到其他活动的类。
Bundle extras = getIntent().getExtras();
ParcalableForm po = new ParcalableForm(extras.getParcelableArrayList("geopoints"));
This is the part where i don't know how to get the object/Arraylist.I have tried many methods but no luck.Any ideas? 这是我不知道如何获取对象/ Arraylist的部分。我尝试了很多方法,但是没有运气。有什么想法吗?
Pass Data to anther activity 将数据传递到花药活动
ArrayList<Animal> animals = new ArrayList<Animal>();
//fill your list with animals here
i.putExtra("animals", animals);
Receiving this data 接收数据
ArrayList<Animal> animals = (ArrayList<Animal>) getIntent()
.getSerializableExtra("animals");
If what you want is to pass ArrayList<Form>
, then use getParcelableArrayListExtra for this. 如果要传递ArrayList<Form>
,则使用getParcelableArrayListExtra 。
Generally, follow these steps : 通常,请按照下列步骤操作:
Form
class properly implement Parcelable 使您的Form
类正确实现Parcelable In activity sending intent : 在活动中发送意图:
// ArrayList<Form> myList - data to send; intent.putParcelableArrayListExtra("geopoints", myList);
In receiving activity : 在接收活动中:
ArrayList<Form> myReceivedList = getIntent().getParcelableArrayListExtra("geopoints");
And don't forget null/sanity checks. 并且不要忘记null /健全性检查。
Hope that helps 希望能有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.