如何使用 Parcelable 在活动之间传递带有其他对象列表的对象?

How to pass object with List of other object between activities using Parcelable?

我有一个名为 Order 的对象,我想在活动之间传递它。目前我正在使用 Parcelable 这样做。

public class Order implements Parcelable {

    private String email;

    private Long timestamp;

    private List<OrderItem> items;

    public Order() { }

    private Order(Parcel in) {
        email = in.readString();
        timestamp = in.readLong();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(email);
        if (timestamp == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(timestamp);
        }
        dest.writeTypedList(items);
    }

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

    public static final Creator<Order> CREATOR = new Creator<Order>() {
        @Override
        public Order createFromParcel(Parcel in) {
            return new Order(in);
        }

        @Override
        public Order[] newArray(int size) {
            return new Order[size];
        }
    };
    // Getters
    ...
}

items 字段是实现 Parcelable 接口的 OrderItem 个对象的 List 个。

public class OrderItem implements Parcelable {

    private String orderedClothingId;

    private int quantity;

    public OrderItem() { }

    public OrderItem(String orderedClothingId, int quantity) {
        this.orderedClothingId = orderedClothingId;
        this.quantity = quantity;
    }

    private OrderItem(Parcel in) {
        orderedClothingId = in.readString();
        quantity = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(orderedClothingId);
        dest.writeInt(quantity);
    }

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

    public static final Creator<OrderItem> CREATOR = new Creator<OrderItem>() {
        @Override
        public OrderItem createFromParcel(Parcel in) {
            return new OrderItem(in);
        }

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

现在要将名为 orderOrder 对象从一个 activity 传递到另一个 activity,我执行以下操作:

Intent intent = new Intent(mContext, ActivityTwo.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(ORDER_DETAIL_INTENT_EXTRA_KEY, order);            
mContext.startActivity(intent);

ActivityTwo 中,我像这样收集 Order 对象:

Bundle data = getIntent().getExtras();
assert data != null;
mOrder = data.getParcelable(ORDER_DETAIL_INTENT_EXTRA_KEY);

但是,当我在 ActivityTwo 中记录包含在 Order 对象中的 items 字段时,它是 null。如何在 items 列表不为空的情况下在活动之间传递原始非空 Order 对象?

乍一看,您似乎在 parcelable 中传递了不同的密钥 第一个 ORDER_DETAIL_INTENT_EXTRA_KEY,第二个 CLOTHING_ADMIN_DETAIL_INTENT_EXTRA_KEY。它们应该是一样的,所以选择一个。

您也可以使用 getIntent().getParcelableExtra() 而不必使用 Bundle

首先你错过了用dest = in.readTypedList(emptyList, CREATOR);

读回数组

但第二个也是更重要的一点是,您需要 write/read 相同数量的参数,因为您在 writeToParcel 中有一个 if ,您在读取时需要相同的参数:

private Order(Parcel in) {
    email = in.readString();
    if(in.readByte() == 1)
        timestamp = in.readLong(); //here to skip just like the writeToParcel
    in.readTypedList(items = new ArrayList<OrderItem>(), OrderItem.CREATOR);
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(email);
    if (timestamp == null) {
        dest.writeByte((byte) 0);
    } else {
        dest.writeByte((byte) 1);
        dest.writeLong(timestamp);
    }
    dest.writeTypedList(items);
}