简体   繁体   中英

How to read/write nested ArrayLists when using Parcelable in Android?

I'm having some trouble parceling a structure like this:

private ArrayList<ArrayList<SomeObject>> objects;

Which comes from a JSON with this structure:

{
  "a" :[
         [
           {}, {}, {}, {}
         ]
       ]
}

My writing to parcel goes like this:

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeList(objects);
}

private Listings(Parcel in) {
    this.objects = new ArrayList<ArrayList<SomeObject>>();
    in.readArrayList(SomeObject.class.getClassLoader());
}

But when unmarshalling i'm only getting "a" : []

Any ideas?

Best Regards.

Three things to check.

First, the most obvious is that you do not assign this.objects to anything except for an empty list. You either need to assign the output of your call to readArrayList() to this.objects or call readList with this.objects as the first parameter as done below.

Second, make sure SomeObject meets the requirements laid out in Parcel.writeValue() .

Third, if you called unmarshall to load the parcel, try adding a call to Parcel.setDataPosition(0) in your listings method to make sure the parcel is set to read from the beginning before you start parsing. Like so --

private Listings(Parcel in) {
    in.setDataPosition(0);
    in.readList(this.object, SomeObject.class.getClassLoader());
}

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