在另一活动传递对象的ArrayList [英] Pass Arraylist of objects in another activity

查看:108
本文介绍了在另一活动传递对象的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发送另一个活动对象的数组列表。我检查了关于这一主题的文章很多,我使用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; ,然后用<一个href="http://developer.android.com/reference/android/content/Intent.html#getParcelableArrayListExtra(java.lang.String)"相对=nofollow> getParcelableArrayListExtra 这一点。
一般情况下,请按照下列步骤操作:

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

  • 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.

希望帮助

这篇关于在另一活动传递对象的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆