You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中如何将Activity的ArrayList<ListeJoueurs>传递至另一Activity?

解决Android中通过Intent传递ArrayList<ListeJoueurs>的问题

要实现用getIntent()传递自定义对象集合,核心是让你的ListeJoueurs类实现序列化接口,Android中的Intent仅支持传递可序列化的数据类型。下面提供两种可行方案:


方案1:实现Serializable接口(简单易实现)

Serializable是Java的序列化标记接口,无需编写额外逻辑,适合快速改造:

步骤1:修改ListeJoueurs类

只需让类实现Serializable接口即可,成员变量(String/Long/int)本身都支持序列化:

public class ListeJoueurs implements Serializable {
    String Nom;
    String Prenom;
    Long Licence;
    String Cat;
    String Select;
    String dteN;
    int imageClick;

    // 原构造方法保留
    public ListeJoueurs(String Nom, String Prenom, Long Licence, String Cat, String Select, String dteN, int imageClick) {
        this.Nom = Nom;
        this.Prenom = Prenom;
        this.Licence = Licence;
        this.Cat = Cat;
        this.Select = Select;
        this.dteN = dteN;
        this.imageClick = imageClick;
    }

    // 原toString方法保留
    @Override
    public String toString() {
        return this.Nom + " " + this.Prenom + " " + this.Licence + " " + this.Cat + " " + this.Select + " " + this.dteN + " " + this.imageClick;
    }
}

步骤2:第一个Activity中传递集合

// 初始化你的ArrayList<ListeJoueurs>
ArrayList<ListeJoueurs> la = new ArrayList<>();
// 添加数据...

// 创建Intent并传递集合
Intent intent = new Intent(当前Activity.this, 目标Activity.class);
intent.putExtra("joueurs_list", la);
startActivity(intent);

步骤3:目标Activity中接收集合

ArrayList<ListeJoueurs> receivedList = (ArrayList<ListeJoueurs>) getIntent().getSerializableExtra("joueurs_list");

方案2:实现Parcelable接口(Android推荐,效率更高)

Parcelable是Android专属的序列化方式,序列化效率远高于Serializable,适合性能敏感场景:

步骤1:修改ListeJoueurs类

实现Parcelable接口,需编写序列化/反序列化逻辑:

public class ListeJoueurs implements Parcelable {
    String Nom;
    String Prenom;
    Long Licence;
    String Cat;
    String Select;
    String dteN;
    int imageClick;

    // 原构造方法保留
    public ListeJoueurs(String Nom, String Prenom, Long Licence, String Cat, String Select, String dteN, int imageClick) {
        this.Nom = Nom;
        this.Prenom = Prenom;
        this.Licence = Licence;
        this.Cat = Cat;
        this.Select = Select;
        this.dteN = dteN;
        this.imageClick = imageClick;
    }

    // 从Parcel中读取数据的构造方法
    protected ListeJoueurs(Parcel in) {
        Nom = in.readString();
        Prenom = in.readString();
        Licence = in.readLong();
        Cat = in.readString();
        Select = in.readString();
        dteN = in.readString();
        imageClick = in.readInt();
    }

    // 必须的CREATOR静态内部类
    public static final Creator<ListeJoueurs> CREATOR = new Creator<ListeJoueurs>() {
        @Override
        public ListeJoueurs createFromParcel(Parcel in) {
            return new ListeJoueurs(in);
        }

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

    @Override
    public int describeContents() {
        return 0; // 无特殊内容需描述,返回0即可
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // 按顺序写入数据
        dest.writeString(Nom);
        dest.writeString(Prenom);
        dest.writeLong(Licence);
        dest.writeString(Cat);
        dest.writeString(Select);
        dest.writeString(dteN);
        dest.writeInt(imageClick);
    }

    // 原toString方法保留
    @Override
    public String toString() {
        return this.Nom + " " + this.Prenom + " " + this.Licence + " " + this.Cat + " " + this.Select + " " + this.dteN + " " + this.imageClick;
    }
}

步骤2:第一个Activity中传递集合

ArrayList<ListeJoueurs> la = new ArrayList<>();
// 添加数据...

Intent intent = new Intent(当前Activity.this, 目标Activity.class);
intent.putParcelableArrayListExtra("joueurs_list", la);
startActivity(intent);

步骤3:目标Activity中接收集合

ArrayList<ListeJoueurs> receivedList = getIntent().getParcelableArrayListExtra("joueurs_list");

方案对比

  • Serializable:代码改动极小,适合快速开发,但序列化效率低。
  • Parcelable:Android官方推荐,序列化/反序列化速度更快,适合大数据量或性能要求高的场景。

内容的提问来源于stack exchange,提问作者Georges GRESSIEN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 14:55:18