传递 ArrayList<CustomObject>活动之间

编程入门 行业动态 更新时间:2024-10-25 22:34:06
本文介绍了传递 ArrayList&lt;CustomObject>活动之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我已经实现了一个 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-16 09:57:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/884916.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:lt   ArrayList   gt   CustomObject

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!