24
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.

fish40
  • 5,738
  • 17
  • 50
  • 69

3 Answers3

36

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);
Sarah Sukardi
  • 45
  • 1
  • 8
Brosa
  • 1,169
  • 9
  • 19
6

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);
}
Pablo Cegarra
  • 20,955
  • 12
  • 92
  • 110
  • 2
    When reading list from parcelable it is better to use `in.readList(myList, MyParcelableClass.class.getClassLoader());` – Wojtek Sep 06 '17 at 20:21
2
public static final Parcelable.Creator<Category> CREATOR = new Parcelable.Creator<Category>() {
            public Category createFromParcel(Parcel in) {
                return new Category(in);
            }

            public Category[] newArray(int size) {
                return new Category[size];
            }
        };

private Category(Parcel in) {
    String[] data = new String[1];
    in.readStringArray(data);
    mCategoryId = Integer.parseInt(data[0]);
}

public void writeToParcel(Parcel dest, int flags) {
    dest.writeStringArray(new String[]{
            mCategoryId
    });
}

Then In ur Activity.

public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelableArrayList("mCategoryVideos", (List<? extends Parcelable>) mCategoryVideos);
}

public void onRestoreInstanceState(Bundle inState) {
    super.onRestoreInstanceState(inState);
    if (inState != null) {
        mCategoryVideos = inState.getParcelableArrayList("mCategoryVideos");
        // Restore All Necessary Variables Here
    }
}
Gautam Chibde
  • 1,167
  • 3
  • 14
  • 27
TheFlash
  • 5,997
  • 4
  • 41
  • 46