将arraylist <class>发送给另一个意图</class>

时间:2012-07-16 17:39:39

标签: android arraylist parcelable

我有这堂课:

public class  Hotel implements Parcelable {

public Hotel() {
    super();
}

public Hotel(Parcel in) {
    this.id = in.readString();
    this.hotelFileName = in.readString();
    this.hotelName = in.readString();
    this.rating = in.readString();
    this.cityId = in.readString();
    this.cityFileName = in.readString();
    this.cityName = in.readString();
    this.stateId = in.readString();
    this.stateFileName = in.readString();
    this.stateName = in.readString();
    this.countryCode = in.readString();
    this.countryFileName = in.readString();
    this.countryName = in.readString();
    this.imageId = in.readString();
    this.address = in.readString();
    this.minRate = in.readString();
    this.currencyCode = in.readString();
    Latitude = in.readString();
    Longitude = in.readString();
    NumberOfReviews = in.readString();
    ConsumerRating = in.readString();
    PropertyType = in.readString();
    ChainID = in.readString();
    Facilities = in.readString();
}

public Hotel(String id, String hotelFileName, String hotelName,
        String rating, String cityId, String cityFileName, String cityName,
        String stateId, String stateFileName, String stateName,
        String countryCode, String countryFileName, String countryName,
        String imageId, String address, String minRate,
        String currencyCode, String latitude, String longitude,
        String numberOfReviews, String consumerRating, String propertyType,
        String chainID, String facilities) {
    super();
    this.id = id;
    this.hotelFileName = hotelFileName;
    this.hotelName = hotelName;
    this.rating = rating;
    this.cityId = cityId;
    this.cityFileName = cityFileName;
    this.cityName = cityName;
    this.stateId = stateId;
    this.stateFileName = stateFileName;
    this.stateName = stateName;
    this.countryCode = countryCode;
    this.countryFileName = countryFileName;
    this.countryName = countryName;
    this.imageId = imageId;
    this.address = address;
    this.minRate = minRate;
    this.currencyCode = currencyCode;
    Latitude = latitude;
    Longitude = longitude;
    NumberOfReviews = numberOfReviews;
    ConsumerRating = consumerRating;
    PropertyType = propertyType;
    ChainID = chainID;
    Facilities = facilities;
}

private String id;
private String hotelFileName;
private String hotelName;
private String rating;
private String cityId;
private String cityFileName;
private String cityName;
private String stateId;
private String stateFileName;
private String stateName;
private String countryCode;
private String countryFileName;
private String countryName;
private String imageId;
private String address;
private String minRate;
private String currencyCode;
private String Latitude;
private String Longitude;
private String NumberOfReviews;
private String ConsumerRating;
private String PropertyType;
private String ChainID;
private String Facilities;
private String distance;

@Override
public String toString() {
    return "Hotel [id=" + id + ", hotelFileName=" + hotelFileName
            + ", hotelName=" + hotelName + ", rating=" + rating
            + ", cityId=" + cityId + ", cityFileName=" + cityFileName
            + ", cityName=" + cityName + ", stateId=" + stateId
            + ", stateFileName=" + stateFileName + ", stateName="
            + stateName + ", countryCode=" + countryCode
            + ", countryFileName=" + countryFileName + ", countryName="
            + countryName + ", imageId=" + imageId + ", address=" + address
            + ", minRate=" + minRate + ", currencyCode=" + currencyCode
            + ", Latitude=" + Latitude + ", Longitude=" + Longitude
            + ", NumberOfReviews=" + NumberOfReviews + ", ConsumerRating="
            + ConsumerRating + ", PropertyType=" + PropertyType
            + ", ChainID=" + ChainID + ", Facilities=" + Facilities + "]";
}

public String getId() {
    return id;
}

public String getHotelFileName() {
    return hotelFileName;
}

public void setHotelFileName(String hotelFileName) {
    this.hotelFileName = hotelFileName;
}

public String getHotelName() {
    return hotelName;
}

public void setHotelName(String hotelName) {
    this.hotelName = hotelName;
}

public String getRating() {
    return rating;
}

public String getCityId() {
    return cityId;
}

public void setCityId(String cityId) {
    this.cityId = cityId;
}

public String getCityFileName() {
    return cityFileName;
}

public void setCityFileName(String cityFileName) {
    this.cityFileName = cityFileName;
}

public String getCityName() {
    return cityName;
}

public void setCityName(String cityName) {
    this.cityName = cityName;
}

public String getStateId() {
    return stateId;
}

public void setStateId(String stateId) {
    this.stateId = stateId;
}

public String getStateFileName() {
    return stateFileName;
}

public void setStateFileName(String stateFileName) {
    this.stateFileName = stateFileName;
}

public String getStateName() {
    return stateName;
}

public void setStateName(String stateName) {
    this.stateName = stateName;
}

public String getCountryCode() {
    return countryCode;
}

public void setCountryCode(String countryCode) {
    this.countryCode = countryCode;
}

public String getCountryFileName() {
    return countryFileName;
}

public void setCountryFileName(String countryFileName) {
    this.countryFileName = countryFileName;
}

public String getCountryName() {
    return countryName;
}

public void setCountryName(String countryName) {
    this.countryName = countryName;
}

public String getImageId() {
    return imageId;
}

public void setImageId(String imageId) {
    this.imageId = imageId;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getMinRate() {
    return minRate;
}

public void setMinRate(String minRate) {
    this.minRate = minRate;
}

public String getCurrencyCode() {
    return currencyCode;
}

public void setCurrencyCode(String currencyCode) {
    this.currencyCode = currencyCode;
}

public String getLatitude() {
    return Latitude;
}

public void setLatitude(String latitude) {
    Latitude = latitude;
}

public String getLongitude() {
    return Longitude;
}

public void setLongitude(String longitude) {
    Longitude = longitude;
}

public String getNumberOfReviews() {
    return NumberOfReviews;
}

public void setNumberOfReviews(String numberOfReviews) {
    NumberOfReviews = numberOfReviews;
}

public String getConsumerRating() {
    return ConsumerRating;
}

public void setConsumerRating(String consumerRating) {
    ConsumerRating = consumerRating;
}

public String getPropertyType() {
    return PropertyType;
}

public void setPropertyType(String propertyType) {
    PropertyType = propertyType;
}

public String getChainID() {
    return ChainID;
}

public void setChainID(String chainID) {
    ChainID = chainID;
}

public String getFacilities() {
    return Facilities;
}

public void setFacilities(String facilities) {
    Facilities = facilities;
}

public void setId(String id) {
    this.id = id;
}

public void setRating(String rating) {
    this.rating = rating;
}

public String getDistance() {
    return distance;
}

public void setDistance(String distance) {
    this.distance = distance;
}

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(id.isEmpty()?"":id);
    dest.writeString(hotelFileName.isEmpty()?"":hotelFileName);
    dest.writeString(hotelName.isEmpty()?"":hotelName);
    dest.writeString(rating.isEmpty()?"":rating);
    dest.writeString(cityId.isEmpty()?"":cityId);
    dest.writeString(cityFileName.isEmpty()?"":cityFileName);
    dest.writeString(cityName.isEmpty()?"":cityName);
    dest.writeString(stateId.isEmpty()?"":stateId);
    dest.writeString(stateFileName.isEmpty()?"":stateFileName);
    dest.writeString(stateName.isEmpty()?"":stateName);
    dest.writeString(countryCode.isEmpty()?"":countryCode);
    dest.writeString(countryFileName.isEmpty()?"":countryFileName);
    dest.writeString(countryName.isEmpty()?"":countryName);
    dest.writeString(imageId.isEmpty()?"":imageId);
    dest.writeString(address.isEmpty()?"":address);
    dest.writeString(minRate.isEmpty()?"":minRate);
    dest.writeString(currencyCode.isEmpty()?"":currencyCode);
    dest.writeString(Latitude.isEmpty()?"":Latitude);
    dest.writeString(Longitude.isEmpty()?"":Longitude);
    dest.writeString(NumberOfReviews.isEmpty()?"":NumberOfReviews);
    dest.writeString(ConsumerRating.isEmpty()?"":ConsumerRating);
    dest.writeString(PropertyType.isEmpty()?"":PropertyType);
    dest.writeString(ChainID.isEmpty()?"":ChainID);
    dest.writeString(Facilities.isEmpty()?"":Facilities);
    dest.writeString(distance.isEmpty()?"":distance);
}

public static final Parcelable.Creator<Hotel> CREATOR = new Parcelable.Creator<Hotel>() {
    public Hotel createFromParcel(Parcel in) {

        return new Hotel(in);
    }

    public Hotel[] newArray(int size) {

        return new Hotel[size];
    }
};

}

我想使用arraylist<Hotel>

从一个意图发送i.putExtra("hotels", hotels);到另一个意图

我收到了arraylist<Hotel>

ArrayList<Hotel> hotels = (ArrayList<Hotel>) i
                .getSerializableExtra("hotels");

请注意,酒店可以包含一些空字段。

执行时,获取此错误:

  

07-16 18:35:59.779:E / AndroidRuntime(6335):致命异常:主要   07-16 18:35:59.779:E / AndroidRuntime(6335):java.lang.RuntimeException:无法启动活动ComponentInfo {suptechnique.hotelsdirectory / com.suptech.hotels.map.CarteActivity}:java.lang.RuntimeException:Parcel android.os.Parcel@40511ae8:在偏移448处解组未知类型代码3473457   07-16 18:35:59.779:E / AndroidRuntime(6335):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)   07-16 18:35:59.779:E / AndroidRuntime(6335):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)   07-16 18:35:59.779:E / AndroidRuntime(6335):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)   07-16 18:35:59.779:E / AndroidRuntime(6335):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:935)   07-16 18:35:59.779:E / AndroidRuntime(6335):在android.os.Handler.dispatchMessage(Handler.java:99)   07-16 18:35:59.779:E / AndroidRuntime(6335):在android.os.Looper.loop(Looper.java:123)   07-16 18:35:59.779:E / AndroidRuntime(6335):在android.app.ActivityThread.main(ActivityThread.java:3687)   07-16 18:35:59.779:E / AndroidRuntime(6335):at java.lang.reflect.Method.invokeNative(Native Method)   07-16 18:35:59.779:E / AndroidRuntime(6335):at java.lang.reflect.Method.invoke(Method.java:507)   07-16 18:35:59.779:E / AndroidRuntime(6335):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867)   07-16 18:35:59.779:E / AndroidRuntime(6335):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)   07-16 18:35:59.779:E / AndroidRuntime(6335):at dalvik.system.NativeStart.main(Native Method)   07-16 18:35:59.779:E / AndroidRuntime(6335):引起:java.lang.RuntimeException:Parcel android.os.Parcel@40511ae8:在偏移量448解组未知类型代码3473457   07-16 18:35:59.779:E / AndroidRuntime(6335):在android.os.Parcel.readValue(Parcel.java:1913)   07-16 18:35:59.779:E / AndroidRuntime(6335):在android.os.Parcel.readListInternal(Parcel.java:2092)   07-16 18:35:59.779:E / AndroidRuntime(6335):在android.os.Parcel.readArrayList(Parcel.java:1536)   07-16 18:35:59.779:E / AndroidRuntime(6335):在android.os.Parcel.readValue(Parcel.java:1867)   07-16 18:35:59.779:E / AndroidRuntime(6335):在android.os.Parcel.readMapInternal(Parcel.java:2083)   07-16 18:35:59.779:E / AndroidRuntime(6335):在android.os.Bundle.unparcel(Bundle.java:208)   07-16 18:35:59.779:E / AndroidRuntime(6335):在android.os.Bundle.getSerializable(Bundle.java:1189)   07-16 18:35:59.779:E / AndroidRuntime(6335):在android.content.Intent.getSerializableExtra(Intent.java:3583)   07-16 18:35:59.779:E / AndroidRuntime(6335):at com.suptech.hotels.map.CarteActivity.onCreate(CarteActivity.java:39)   07-16 18:35:59.779:E / AndroidRuntime(6335):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)   07-16 18:35:59.779:E / AndroidRuntime(6335):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)   07-16 18:35:59.779:E / AndroidRuntime(6335):... 11更多

2 个答案:

答案 0 :(得分:1)

让您的酒店课程实施Serializable而不是Parcelable

答案 1 :(得分:1)

implements Parcelable替换为implements Serializable ..它会像魅力一样!