繁体   English   中英

如何使用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对象从一个活动传递到另一个活动,请执行以下操作:

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 如何在活动之间传递原始的非空Order对象,而items列表不为空?

乍一看,您ORDER_DETAIL_INTENT_EXTRA_KEY在第一个和第二个CLOTHING_ADMIN_DETAIL_INTENT_EXTRA_KEY打包ORDER_DETAIL_INTENT_EXTRA_KEY中传递了不同的密钥。 他们应该都一样,所以选择哪一个。

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

首先,您错过了使用dest = in.readTypedList(emptyList, CREATOR);读取数组的操作dest = in.readTypedList(emptyList, CREATOR);

但是第二点,更重要的是,您需要编写/读取相同数量的参数,因为在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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM