【问题标题】:Reading and writing java.util.ArrayList from Parcelable class从 Parcelable 类中读取和写入 java.util.ArrayList
【发布时间】:2021-08-11 22:00:24
【问题描述】:

我正在使用Parcelable 类将Object 从一个活动发送到 Android Java 中的另一个活动。

Parcelable 只为我实现 String 数据。

我如何在这门课上读写productsArrayListstatus

public class Bill implements Parcelable {
    private String id;
    private Date createAt;
    private ArrayList<Products> productsArrayList;
    private ArrayList<StatusBill> status;

    public Bill(String id, Date createAt, ArrayList<Products> productsArrayList, ArrayList<StatusBill> status) {
        this.id = id;
        this.createAt = createAt;
        this.productsArrayList = productsArrayList;
        this.status = status;
    }

    protected Bill(Parcel in) {
        id = in.readString();
        createAt = new Date(in.readLong());
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(id);
        dest.writeLong(createAt.getTime());
    }
    public static final Creator<Bill> CREATOR = new Creator<Bill>() {
        @Override
        public Bill createFromParcel(Parcel in) {
            return new Bill(in);
        }

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

【问题讨论】:

标签: java class arraylist android-intent parcelable


【解决方案1】:

我在使用 Parcelable 类时找到了适用于所有情况的解决方案

本网站帮助您实现Custom 类到Parcelable

Custom 类粘贴到 textarea 中,然后单击 Build 按钮。

您将收到Custom 类实现Parcelable

http://www.parcelabler.com/

【讨论】:

  • 应该有另一种解决方案:将列表序列化为 JSON 字符串。
猜你喜欢
  • 2014-01-27
  • 1970-01-01
  • 1970-01-01
  • 2013-03-10
  • 2018-04-03
  • 2019-03-03
  • 2015-07-07
  • 1970-01-01
  • 2015-05-06
相关资源
最近更新 更多