0

I have a class called ClassA which contains a List of ClassB objects. Both classes implement Parcelable.

I am trying to create the read/write methods for this list in ClassA and am having trouble. For example I tried:

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeList(mClassBList);
}

but then

private ClassA(Parcel in) { 
    mClassBList = in.readList();
}

throws an error because it needs all these extra arguments.

How do I correctly read/write this List?

KaliMa
  • 1,970
  • 6
  • 26
  • 51
  • What is the error it is throwing? – buczek Apr 18 '16 at 21:05
  • 2
    [This answer might also help](http://stackoverflow.com/questions/6300608/how-to-pass-a-parcelable-object-that-contains-a-list-of-objects) – buczek Apr 18 '16 at 21:06
  • "throws an error because it needs all these extra arguments" -- well, that method returns `void`. You don't assign it to anything. You pass the `List` as a parameter, rather than getting it returned to you. – CommonsWare Apr 18 '16 at 21:24

1 Answers1

2

The links suggested by both George and buczek can be really helpful in your case. Perhaps more directly, you need to do something like this:

//Supposing you declared mClassBList as:
List<ClassB> mClassBList = new ArrayList<ClassB>(); 
in.readTypedList(mClassBList, ClassB.CREATOR);

I still recommend you check out the answer, especially (as suggested by Buczek) How to pass a parcelable object that contains a list of objects?

Community
  • 1
  • 1
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32