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

后端 未结 9 1005
眼角桃花
眼角桃花 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:37

    Product must be implements Parcelable

      Product class implements  Parcelable {
              ......
      }
    

    Then write you object contains list like

    public class Outfits implements Parcelable {
    
         private String url;
         private String name;
         private List products;
    
         public Outfits (Parcel pi) {
    
            bookName = p.readString();
            bookId = p.readInt();
            isColor = p.readInt() == 1;
    
            //use this well be get err
            //products=p.readArrayList(Thread.currentThread().getContextClassLoader());
    
            //Pass list use this 
            products= in.createTypedArrayList(Product.CREATOR);
    
          }
    
                ...get and set...
    
         public void writeToParcel(Parcel dest, int flags) {
    
    
            dest.writeString(url);
            dest.writeString(name);
    
            //use this will no working
            //dest.writeList(products);
    
            //Parcelable list
            out.writeTypedList(products);
    
         }
    
     }
    

提交回复
热议问题