如何通过在包含对象的列表的parcelable对象? [英] How to pass a parcelable object that contains a list of objects?

查看:137
本文介绍了如何通过在包含对象的列表的parcelable对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个parcelable对象下面,我对象包含的产品清单。在我的构造如何处理重新创建我parcelable的列表?

我已经检查了所有可以从包裹的方法和所有可用的readArrayList(类加载器)。林不知道这是否是最好的方法,你的建议真的会AP preciated。

 公共类服装实现Parcelable {

    私人字符串URL;
    私人字符串名称;
    私人列表<产品>产品;

    公共字符串的getURL(){
        返回URL;
    }
    公共无效setUrl(字符串URL){
        this.url =网址;
    }
    公共字符串的getName(){
        返回名称;
    }
    公共无效setname可以(字符串名称){
        this.name =名称;
    }
    公开名单<产品>的getProducts(){
        回报的产品;
    }
    公共无效setProducts(名单<产品>产品){
        this.products =产品;
    }

    公共无效writeToParcel(包裹DEST,INT标志){
        Log.v(,writeToParcel ...+标志);
        dest.writeString(URL);
        dest.writeString(名称);
        dest.writeList(产品);
    }


    公共静态最终Parcelable.Creator CREATOR =新Parcelable.Creator(){
        公共装备createFromParcel(包裹中){
            返回新衣服(中);
        }

        公共装备[] newArray(INT尺寸){
            返回新衣服[大小]
        }
    };

    @覆盖
    公众诠释describeContents(){
        返回0;
    }

    / ***在这里,我怎么填充我的产品列表*** /
    私人服装(包裹中){
        URL = in.readString();
        名称= in.readString();
        产品= in.read ???????;
    }
}
 

解决方案

如果类产品与parcelable协议兼容,下面应根据文档工作。

 产品=新的ArrayList<产品>();
in.readList(产品,NULL);
 

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?

I have checked all of the methods available from the parcel and all that is available is readArrayList(ClassLoader). Im 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 ???????;
    }
}

解决方案

If class Product is compatible with parcelable protocol, following should work according to documentation.

products = new ArrayList<Product>();
in.readList(products,null);

这篇关于如何通过在包含对象的列表的parcelable对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆