我使用Parcelable实现损坏了数据

时间:2016-08-20 17:51:17

标签: android parcelable

我已为Parcelable实施了RealEstateAd界面。但是,我的数据在活动之间的每次转移中都会被破坏。

初始数据(将传递给活动#2):

classOfProperty = 101
typeOfAd = 102 <-- Note this value.
typeOfAdvertiser = 101
typeOfProperty = 101

活动#2中收到的数据:

classOfProperty = 101
typeOfAd = 101 <-- It should be 102
typeOfAdvertiser = 101
typeOfProperty = 102 <-- It should be 101

我的Parcelable实施:

// Parcelable implementation

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

@Override
public void writeToParcel(Parcel out, int flags) {
    out.writeString(uid);
    out.writeString(id);

    out.writeInt(status);

    out.writeString(publishDate);
    out.writeString(modifiedDate);
    out.writeString(expiryData);

    out.writeString(publishIp);
    out.writeString(modifiedIp);

    out.writeString(number);

    out.writeInt(rank);
    out.writeLong(hits);

    out.writeInt(numberOfComments);
    out.writeInt(numberOfCreditsUnused);
    out.writeInt(numberOfCreditsUsed);

    out.writeString(title);
    out.writeString(content);

    out.writeDouble(price);

    out.writeInt(methodOfPayment);

    out.writeInt(typeOfAd);
    out.writeInt(typeOfProperty);
    out.writeInt(typeOfAdvertiser);
    out.writeInt(classOfProperty);

    out.writeString(mobile);

    out.writeString(street);
    out.writeString(neighborhood);
    out.writeString(city);
    out.writeString(province);
    out.writeString(country);

    out.writeDouble(latitude);
    out.writeDouble(longitude);

    out.writeInt(numberOfRooms);
    out.writeInt(numberOfBaths);
    out.writeInt(numberOfHalls);
    out.writeInt(numberOfKitchens);
    out.writeInt(numberOfFloors);
    out.writeInt(numberOfInternalStairs);
    out.writeInt(numberOfExternalStairs);
    out.writeInt(numberOfSwimmingPools);
    out.writeInt(numberOfFireplace);
    out.writeInt(numberOfTents);
    out.writeInt(numberOfMaidRooms);
    out.writeInt(numberOfDriverRooms);
    out.writeInt(numberOfStorageRooms);
    out.writeInt(numberOfStores);
    out.writeInt(numberOfApartments);
    out.writeInt(numberOfPalms);
    out.writeInt(numberOfWells);
    out.writeInt(numberOfGuardRooms);

    out.writeInt(hasGarage ? 1 : 0);
    out.writeInt(hasCourtyard ? 1 : 0);
    out.writeInt(hasFamilySection ? 1 : 0);
    out.writeInt(hasSoccerStadium ? 1 : 0);
    out.writeInt(hasVolleyballStadium ? 1 : 0);
    out.writeInt(hasPlayground ? 1 : 0);

    out.writeInt(yearOfConstruction);

    out.writeString(floorNumber);
    out.writeString(finishing);
    out.writeString(area);
    out.writeString(lookout);
}

public static final Parcelable.Creator<RealEstateAd> CREATOR = new Parcelable.Creator<RealEstateAd>() {
    @Override
    public RealEstateAd createFromParcel(Parcel source) {
        return new RealEstateAd(source);
    }

    @Override
    public RealEstateAd[] newArray(int size) {
        return new RealEstateAd[size];
    }
};

private RealEstateAd(Parcel in) {
    uid = in.readString();
    id = in.readString();

    status = in.readInt();

    publishDate = in.readString();
    modifiedDate = in.readString();
    expiryData = in.readString();

    publishIp = in.readString();
    modifiedIp = in.readString();

    number = in.readString();

    rank = in.readInt();
    hits = in.readInt();

    numberOfComments = in.readInt();
    numberOfCreditsUnused = in.readInt();
    numberOfCreditsUsed = in.readInt();

    title = in.readString();
    content = in.readString();

    price = in.readDouble();

    methodOfPayment = in.readInt();

    typeOfAd = in.readInt();
    typeOfProperty = in.readInt();
    typeOfAdvertiser = in.readInt();
    classOfProperty = in.readInt();

    mobile = in.readString();

    street = in.readString();
    neighborhood = in.readString();
    city = in.readString();
    province = in.readString();
    country = in.readString();

    latitude = in.readDouble();
    longitude = in.readDouble();

    numberOfRooms = in.readInt();
    numberOfBaths = in.readInt();
    numberOfHalls = in.readInt();
    numberOfKitchens = in.readInt();
    numberOfFloors = in.readInt();
    numberOfInternalStairs = in.readInt();
    numberOfExternalStairs = in.readInt();
    numberOfSwimmingPools = in.readInt();
    numberOfFireplace = in.readInt();
    numberOfTents = in.readInt();
    numberOfMaidRooms = in.readInt();
    numberOfDriverRooms = in.readInt();
    numberOfStorageRooms = in.readInt();
    numberOfStores = in.readInt();
    numberOfApartments = in.readInt();
    numberOfPalms = in.readInt();
    numberOfWells = in.readInt();
    numberOfGuardRooms = in.readInt();

    hasGarage = in.readInt() == 1;
    hasCourtyard = in.readInt() == 1;
    hasFamilySection = in.readInt() == 1;
    hasSoccerStadium = in.readInt() == 1;
    hasVolleyballStadium = in.readInt() == 1;
    hasPlayground = in.readInt() == 1;

    yearOfConstruction = in.readInt();

    floorNumber = in.readString();
    finishing = in.readString();
    area = in.readString();
    lookout = in.readString();
}

1 个答案:

答案 0 :(得分:1)

似乎 hits 的值在writeToParcel方法中写为Long,但在Parcel构造函数中读为 Int