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

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

    First, your Product object must implements Parcelable.

    And then, use dest.writeTypedList(products) in writeToParcel() method.

    Finally, use following code to parse the list:

    products = new ArrayList<Product>();
    in.readTypedList(products, Product.CREATOR);
    

    For more infomation, please reference the Official Document:

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

    This should work:

    in.readTypedList(products, Product.CREATOR);
    
    0 讨论(0)
  • 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<Product> 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);
    
         }
    
     }
    
    0 讨论(0)
  • 2020-12-04 08:42

    In my personal experience, http://www.parcelabler.com/ is an amazing site for this. You just create your class, and copy paste it into the website, and it generates a Parcelable version of your class.

    I tested it with a class named "Theme" that contained the following variables:

    private String name;
    private int image;
    private List<Card> cards;
    

    The writeToParcel function becomes:

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(image);
        if (cards == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeList(cards);
        }
    }
    

    generated constructor:

    protected Theme(Parcel in) {
        name = in.readString();
        image = in.readInt();
        if (in.readByte() == 0x01) {
            cards = new ArrayList<Card>();
            in.readList(cards, Card.class.getClassLoader());
        } else {
            cards = null;
        }
    }
    

    EDIT: make sure that the Card object is also Parcelable!

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

    Assuming the Product is implementing Parcelable, you can use this for writing:

    dest.writeValue(products);
    

    and this for reading:

    products = (List<Product>) in.readValue(Product.class.getClassLoader());
    
    0 讨论(0)
  • 2020-12-04 08:50

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

    products = new ArrayList<Product>();
    in.readList(products, Product.class.getClassLoader());
    
    0 讨论(0)
提交回复
热议问题