1

I have a Parcelable object that has a list of Parcelable objects. I am trying to read that list back after it has been passed from one Activity to the next, but only the first element is "un-bundled"

public class MyBundle implements Parcelable {
    private List<Data> dataList;

    public static final Parcelable.Creator<MyBundle> CREATOR = new Parcelable.Creator<MyBundle>() {
        public MyBundle createFromParcel(Parcel in) {
            return new MyBundle(in);
        }

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

    public MyBundle() {
    }

    public MyBundle(Parcel in) {
        //dataList = new ArrayList<>();
        //in.readTypedList(dataList, Data.CREATOR);
        dataList = in.createTypedArrayList(Data.CREATOR);
        //BOTH have the same result
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        if (dataList != null && dataList.size() > 0) {
            dest.writeTypedList(dataList);
        }
    }
}

The data object:

/*BaseObject has the following properties:
    UUID uuid;
    long databaseId;
    createdDate;
    modifiedDate;
*/
public class Data extends BaseObject implements Parcelable {
    private String name;
    private String serial;
    private String location;

    public Data() {}

    private Data(Parcel in) {
        String uuidString = in.readString();
        if (uuidString == null) return; //this is null!
        uuid = UUID.fromString(idString);
        databaseId = in.readLong();
        createdDate = new Date(in.readLong());
        modifiedDate = new Date(in.readLong());
        location = in.readString();

        name = in.readString();
        serial = in.readString();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(uuid.toString());
        dest.writeLong(databaseId);
        dest.writeLong(createdDate.getTime());
        dest.writeLong(modifiedDate.getTime());

        dest.writeString(name);
        dest.writeString(serial);
    }

    public static final Parcelable.Creator<Data> CREATOR
            = new Parcelable.Creator<Data>() {
        public Data createFromParcel(Parcel in) {
            return new Data(in);
        }

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

What I have tried:

Quintin Balsdon
  • 5,484
  • 10
  • 54
  • 95

1 Answers1

0

So this is the answer: My Data parcelable misses the location element when it creates the parcel. This obviously results in some kind of offset error when READING occurs. So the coded solution is as follows:

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(uuid.toString());
        dest.writeLong(databaseId);
        dest.writeLong(createdDate.getTime());
        dest.writeLong(modifiedDate.getTime());
        dest.writeString(location); /*HERE!*/
        dest.writeString(name);
        dest.writeString(serial);
    }

I hope this helps someone else.

Quintin Balsdon
  • 5,484
  • 10
  • 54
  • 95