[英]How to pass ArrayList of object between activities using 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];
}
};
}
现在要将一个名为order
的Order
对象从一个活动传递到另一个活动,请执行以下操作:
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.