Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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). I'm 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 ???????;     } } 
like image 564
Byron Avatar asked Jun 09 '11 23:06

Byron


People also ask

What is Parcelable used for?

A Parcelable is the Android implementation of the Java Serializable. It assumes a certain structure and way of processing it. This way a Parcelable can be processed relatively fast, compared to the standard Java serialization.

How do you make an object Parcelable?

Create Parcelable class without plugin in Android Studioimplements Parcelable in your class and then put cursor on "implements Parcelable" and hit Alt+Enter and select Add Parcelable implementation (see image). that's it.


2 Answers

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

products = new ArrayList<Product>(); in.readList(products, Product.class.getClassLoader()); 
like image 199
Alex Gitelman Avatar answered Sep 25 '22 02:09

Alex Gitelman


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:

like image 38
codezjx Avatar answered Sep 22 '22 02:09

codezjx