传递 ArrayList<CustomObject>活动之间 [英] Passing ArrayList&lt;CustomObject&gt; Between Activities

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

问题描述

我已经实现了一个 Parcelable 类:

i've implemented a Parcelable class:

public class Evento implements Parcelable {
    private int;
    private String ;
    private String imagen;
    //more atts

    //Constructors, setters and getters

public Evento(Parcel in) {
    this.id = in.readInt();
    this.titulo = in.readString();
    this.imagen=in.readString();

public void writeToParcel(Parcel dest, int flags) {     
    dest.writeInt(id);
    dest.writeString(titulo);
    dest.writeString(imagen);       
}
public static final Parcelable.Creator<Evento> CREATOR = new Parcelable.Creator<Evento>() {
    @Override
    public Evento createFromParcel(Parcel in) {
        return new Evento(in);
    }

    @Override
    public Evento[] newArray(int size) {
        return new Evento[size];
    }
};

在我的活动中,我试图将一个 arrayList 传递给另一个活动:我覆盖了 onItemClick 并以这种方式传递信息:

In my activity, i'm trying to pass an arrayList to an other activitie: I'me overwriten onItemClick and i'm passing the information this way:

public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            //create new activitie
            ArrayList<Eventos> eventos = new ArrayList<Eventos>();
            Intent intent = new Intent(QueActivity.this, ProductosCategorias.class);

            //Create information
            Bundle informacion= new Bundle();
            informacion.putParcelableArrayList("eventos", eventos);
            intent.putExtras(informacion);

并在其他活动中接收它:

and receiving it in the other activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_productos_categorias);     
    lv = (ListView) findViewById(R.id.listaCategoriaElegida);
    Bundle informacionRecibida = getIntent().getExtras();               
    ArrayList<Evento> eventos =new ArrayList<Evento>();
    eventos= informacionRecibida.getParcelableArrayList("eventos");

我收到空指针异常.我已经调试了它,当我执行 getParcelableArrayList("eventos") 时我得到了 NULL,我的意思是,我的 arraylist eventos 是空的,所以,我没有收到任何信息.

I'm getting a null pointer exception. I've debbuged it and i'm getting NULL when i execute the getParcelableArrayList("eventos"), i mean, my arraylist eventos is null, so, i'm receiving no information.

任何帮助都会很棒

推荐答案

这段代码

Bundle informacion= new Bundle();
informacion.putParcelableArrayList("eventos", ArrayList<Eventos> eventos);
intent.putExtras(informacion);

应该是

Bundle informacion = new Bundle();
ArrayList<Eventos> mas = new ArrayList<Eventos>();
informacion.putSerializable("eventos", mas);
intent.putExtras(informacion);

并确保您的 Eventos 结构像一个可序列化的对象

and Make sure your Eventos structure like a serializable object

private class Eventos implements Serializable {

}

阅读价值观

ArrayList<Eventos> madd=getIntent().getSerializableExtra(key);

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

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