简体   繁体   中英

Writing/reading an ArrayList of Parcelable objects

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 ?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM