在另一活动传递对象的ArrayList对象、ArrayList

2023-09-12 22:26:48 作者:万众扶我为王

我想发送另一个活动对象的数组列表。我检查了关于这一主题的文章很多,我使用parcelable,但我坚持地步,我不能发送object.I已经尝试了许多things.This的是事情,我想。

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];
    }

};

}

这是实现Parcelable.I我试图发送形式的ArrayList到另一个活动的parcelable类。

This is the parcelable class that implements Parcelable.I am trying to send Arraylist of form to another activity.

  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"));

这是部分地方我不知道如何让对象/ Arraylist.I已经试过很多方法,但没有luck.Any想法?

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&LT;表格&GT; ,然后用 getParcelableArrayListExtra 这一点。 一般情况下,请按照下列步骤操作:

If what you want is to pass ArrayList<Form>, then use getParcelableArrayListExtra for this. Generally, follow these steps :

请您表格类正确实施Parcelable

在活动发送意图: Make your Form class properly implement Parcelable

In activity sending intent :

// ArrayList的&LT;表格&GT; myList上 - 要发送的数据; intent.putParcelableArrayListExtra(geopoints,myList中);

// ArrayList<Form> myList - data to send; intent.putParcelableArrayListExtra("geopoints", myList);

在接收活动:

的ArrayList&LT;表格&GT; myReceivedList = getIntent()getParcelableArrayListExtra(geopoints);

不要忘了空/完整性检查。

And don't forget null/sanity checks.

希望帮助