Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write List<> into parcel

Tags:

public class Category implements Parcelable {      private int mCategoryId;     private List<Video> mCategoryVideos;   public int getCategoryId() {         return mCategoryId;     }      public void setCategoryId(int mCategoryId) {         this.mCategoryId = mCategoryId;     }   public List<Video> getCategoryVideos() {         return mCategoryVideos;     }      public void setCategoryVideos(List<Video> videoList) {         mCategoryVideos = videoList;     }  @Override     public void writeToParcel(Parcel parcel, int i) {         parcel.writeInt(mCategoryId);         parcel.writeTypedList(mCategoryVideos);     }      public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {         public Category createFromParcel(Parcel parcel) {             final Category category = new Category();              category.setCategoryId(parcel.readInt());             category.setCategoryVideos(parcel.readTypedList()); */// **WHAT SHOULD I WRITE HERE***              return category;         }          public Category[] newArray(int size) {             return new Category[size];         }     };  } 

Im my code I am using model which implements from parcelable...Could anyone tell em what shout I write in this line category.setCategoryVideos(parcel.readTypedList()) I couldn't find any helpful post.

EDIT: category.setCategoryVideos(parcel.readTypedList(mCategoryVideos,Video.CREATOR)); in here mCategoryVideos I have cannot resolve error.

like image 855
fish40 Avatar asked Mar 21 '13 08:03

fish40


2 Answers

There are list methods for Parcelable class, you can take a look at them here:

readList (List outVal, ClassLoader loader)

writeList (List val)

In your case it would look like:

List<Object> myList = new ArrayList<>();  parcel.readList(myList,List.class.getClassLoader()); category.setCategoryVideos(myList); 
like image 196
Brosa Avatar answered Sep 20 '22 21:09

Brosa


From a good answer:

Easy steps:

private List<MyParcelableClass> mList;  protected MyClassWithInnerList(Parcel in) {     mList = in.readArrayList(MyParcelableClass.class.getClassLoader()); } @Override public void writeToParcel(Parcel dest, int flags) {     dest.writeList(mList); } 
like image 39
Pablo Cegarra Avatar answered Sep 20 '22 21:09

Pablo Cegarra