How to pass a sparse object containing a list of objects?

I created a Parcelable object below, my object contains a List products. In my constructor, how do I handle the recreation of my Parcelable for List ?

I checked all the methods available from the package, and all that is available is readArrayList(ClassLoader) . I am not sure if this is the best approach, your advice will be really 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 ???????; } } 
+87
android android-intent android-activity
Jun 09 '11 at 23:59
source share
7 answers

If the Product class is compatible with the protocol suite, the following should work as described in the documentation.

 products = new ArrayList<Product>(); in.readList(products, Product.class.getClassLoader()); 
+94
Jun 10 2018-11-11T00:
source share
— -

First, your Product object must implement Parcelable .

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

Finally, use the following code to parse the list:

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

For more information, refer to the white paper :

+37
Jul 01 '15 at 9:05
source share

This should work:

 in.readTypedList(products, Product.CREATOR); 
+5
Jun 03 '15 at 8:51
source share

In my personal experience, http://www.parcelabler.com/ is a great site to do this. You simply create your class and copy, paste it into the website, and it generates a Parcelable version of your class.

I tested it with the "Theme" class, which 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 the map object is also Parcelable!

+5
Jul 27 '18 at 11:51
source share

implement Parcelable in the Product class and then

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

if any of the above solutions does not work.

+1
Jan 30 '17 at 17:41
source share

Another 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**); } 

List items must implement Parcelable

+1
Sep 14 '17 at 5:43 on
source share

Here you go...

Make sure "Products.java" needs to be expanded 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); } 
+1
Jan 26 '19 at 8:02
source share



All Articles