代码之家  ›  专栏  ›  技术社区  ›  Quintin Balsdon

Parcelable with List only反序列化第一个元素

  •  1
  • Quintin Balsdon  · 技术社区  · 8 年前

    我有一个可包裹的对象,它有一个可包裹对象的列表。在列表从一个活动传递到下一个活动后,我试图读回该列表,但只有第一个元素是“未绑定的”

    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);
            }
        }
    }
    

    /*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];
            }
        };
    }
    

    我所尝试的:

    1 回复  |  直到 8 年前
        1
  •  0
  •   Quintin Balsdon    8 年前

    这就是答案:我的Data parcelable在创建地块时遗漏了位置元素。这显然会在读取时导致某种偏移误差。因此,编码的解决方案如下:

        @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);
        }