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

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

问题描述

我正在尝试在另一个活动中发送一个对象数组列表.我已经检查了很多关于这个主题的文章,我正在使用 Parcelable,但我被困在无法发送对象的地方.我尝试了很多东西.这就是我正在尝试的东西.

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 的 Parcelable 类.我正在尝试将表单的 Arraylist 发送到另一个活动.

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的部分.我尝试了很多方法但没有运气.有什么想法吗?

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

,那么使用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 :

//ArrayListmyList - 要发送的数据;intent.putParcelableArrayListExtra("geopoints", myList);

在接收活动中:

ArrayListmyReceivedList = getIntent().getParcelableArrayListExtra("geopoints");

并且不要忘记空值/健全性检查.

And don't forget null/sanity checks.

希望有帮助

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

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