How to pass a parcelable object that contains a list of objects?

后端 未结 9 982
眼角桃花
眼角桃花 2020-12-04 08:12

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<

相关标签:
9条回答
  • 2020-12-04 08:50

    implement Parcelable on the Product class as well and then

    in.readList(this.list, Product.class.getClassLoader());

    if any of the above solutions didn't work.

    0 讨论(0)
  • 2020-12-04 08:54

    The other way is to use readValue&writeValue.

    protected Product (Parcel in) {
            ...
            in.readValue(this.getClass().getClassLoader());
        }
    
    @Override
    public void writeToParcel(Parcel parcel, int i) {
        ...
        parcel.writeValue(**list**);
    }
    

    Items of the list should implement Parcelable

    0 讨论(0)
  • 2020-12-04 08:58

    Here you go...

    Make sure "Products.java" should be extended with Parcelable

    Step 1:

     private List<Products> products; 
    

    Step 2:

    private Outfits(Parcel in) {
        products = in.createTypedArrayList(Products.CREATOR);
    }
    

    Step 3:

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeTypedList(products);
        }
    
    0 讨论(0)
提交回复
热议问题