如何传递包含对象列表的 Parcelable 对象? [英] How to pass a parcelable object that contains a list of objects?
本文介绍了如何传递包含对象列表的 Parcelable 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在下面创建了一个 Parcelable
对象,我的对象包含一个 List
产品.在我的构造函数中,我如何处理为 List
重新创建我的 Parcelable
?
I have created a Parcelable
object below, my object contains a List
of Products. In my constructor how do I handle re-creating my Parcelable
for the List
?
我已经检查了包中所有可用的方法,所有可用的方法是 readArrayList(ClassLoader)
.我不确定这是否是最好的方法,您的建议将不胜感激.
I have checked all of the methods available from the parcel and all that is available is readArrayList(ClassLoader)
. I'm not sure if this is the best approach, your advice would really be appreciated.
public class Outfits implements Parcelable {
private String url;
private String name;
private List<Product> products;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
public void writeToParcel(Parcel dest, int flags) {
Log.v("", "writeToParcel..." + flags);
dest.writeString(url);
dest.writeString(name);
dest.writeList(products);
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Outfits createFromParcel(Parcel in) {
return new Outfits(in);
}
public Outfits[] newArray(int size) {
return new Outfits[size];
}
};
@Override
public int describeContents() {
return 0;
}
/*** Here how do I populate my List of Products ***/
private Outfits(Parcel in) {
url = in.readString();
name = in.readString();
products = in.read ???????;
}
}
推荐答案
如果 class Product
与 Parcelable 协议兼容,则应根据文档进行以下操作.
If class Product
is compatible with parcelable protocol, following should work according to documentation.
products = new ArrayList<Product>();
in.readList(products, Product.class.getClassLoader());
这篇关于如何传递包含对象列表的 Parcelable 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文