如何在android中实现Parcelable?

时间:2014-10-25 14:40:03

标签: java android

你能告诉我如何在android中实现Parcelable吗?实际上我想从一个活动发送一个数组列表到另一个活动。所以我谷歌发现需要Parcelable点或模型。但是我有另一个类的对象所以我怎么用Parcelable写?换句话说 我有这个对象

DestinationStation destinationStation;

我应该在这个function public void writeToParcel

中写些什么

dest.writeString(destinationStation);

这是我的两个类的代码

public class deparaturedaseboarddto  implements Parcelable{
     ArrayList<deparaturedaseboarddto> data;

        public ArrayList<deparaturedaseboarddto> getData() {
            return data;
        }

        public void setData(ArrayList<deparaturedaseboarddto> data) {
            this.data = data;
        }

    @SerializedName("alertsId")
 int alertsId;
    @SerializedName("destExpArrival")
 String destExpArrival;
    @SerializedName("destSchArrival")
 String destSchArrival;
    @SerializedName("expDepart")
 String expDepart;
    @SerializedName("filteredStation")
 String filteredStation;
    @SerializedName("platformNo")
 String platformNo;
    @SerializedName("rid")
 String rid;
    @SerializedName("schDepart")
 String schDepart;
    @SerializedName("toc")
 String toc;
    @SerializedName("toExpArrival")
 String toExpArrival;
    @SerializedName("toSchArrival")
 String toSchArrival;
    @SerializedName("trainID")
 String trainID;
    @SerializedName("trainLastReportedAt")
 String trainLastReportedAt;
    @SerializedName("destinationStation")
 DestinationStation destinationStation;
    public deparaturedaseboarddto(String trainID,String toc,String trainLastReportedAt, String platformNo, String schDepart, String expDepart, int alertsId, String rid, String destSchArrival, String filteredStation, String destExpArrival, String toSchArrival, String toExpArrival,DestinationStation destinationStation){
        super();
        this.trainID=trainID;
        this.toc=toc;
        this.trainLastReportedAt=trainLastReportedAt;
        this.platformNo=platformNo;
        this.schDepart=schDepart;
        this.expDepart=expDepart;
        this.alertsId=alertsId;
        this.destinationStation=destinationStation;
        this.toExpArrival=toExpArrival;
        this.toSchArrival=toSchArrival;
        this.destExpArrival=destExpArrival;
        this.filteredStation=filteredStation;
        this.destSchArrival=destSchArrival;
        this.rid=rid;

    }
public DestinationStation getDestinationStation() {
    return destinationStation;
}
public void setDestinationStation(DestinationStation destinationStation) {
    this.destinationStation = destinationStation;
}
public int getAlertsId() {
    return alertsId;
}
public void setAlertsId(int alertsId) {
    this.alertsId = alertsId;
}
public String getDestExpArrival() {
    return destExpArrival;
}
public void setDestExpArrival(String destExpArrival) {
    this.destExpArrival = destExpArrival;
}
public String getDestSchArrival() {
    return destSchArrival;
}
public void setDestSchArrival(String destSchArrival) {
    this.destSchArrival = destSchArrival;
}
public String getExpDepart() {
    return expDepart;
}
public void setExpDepart(String expDepart) {
    this.expDepart = expDepart;
}
public String getFilteredStation() {
    return filteredStation;
}
public void setFilteredStation(String filteredStation) {
    this.filteredStation = filteredStation;
}
public String getPlatformNo() {
    return platformNo;
}
public void setPlatformNo(String platformNo) {
    this.platformNo = platformNo;
}
public String getRid() {
    return rid;
}
public void setRid(String rid) {
    this.rid = rid;
}
public String getSchDepart() {
    return schDepart;
}
public void setSchDepart(String schDepart) {
    this.schDepart = schDepart;
}
public String getToc() {
    return toc;
}
public void setToc(String toc) {
    this.toc = toc;
}
public String getToExpArrival() {
    return toExpArrival;
}
public void setToExpArrival(String toExpArrival) {
    this.toExpArrival = toExpArrival;
}
public String getToSchArrival() {
    return toSchArrival;
}
public void setToSchArrival(String toSchArrival) {
    this.toSchArrival = toSchArrival;
}
public String getTrainID() {
    return trainID;
}
public void setTrainID(String trainID) {
    this.trainID = trainID;
}
public String getTrainLastReportedAt() {
    return trainLastReportedAt;
}
public void setTrainLastReportedAt(String trainLastReportedAt) {
    this.trainLastReportedAt = trainLastReportedAt;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(alertsId);
dest.writeString(destExpArrival);
dest.writeString(destSchArrival);

dest.writeString(expDepart);
dest.writeString(filteredStation);

dest.writeString(platformNo);
dest.writeString(rid);
dest.writeString(schDepart);
dest.writeString(toc);
dest.writeString(toExpArrival);
dest.writeString(toSchArrival);
dest.writeString(trainID);
dest.writeString(trainLastReportedAt);
dest.writeString(destinationStation);

}

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

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

    private deparaturedaseboarddto(Parcel in) {
        this.alertsId=in.readInt();
        this.destExpArrival=in.readString();
        this.destSchArrival=in.readString();
        this.expDepart=in.readString();
        this.filteredStation=in.readString();
        this.platformNo=in.readString();
        this.rid=in.readString();
        this.schDepart=in.readString();
        this.toc=in.readString();
        this.toExpArrival=in.readString();
        this.toSchArrival=in.readString();
        this.trainID=in.readString();
        this.trainLastReportedAt=in.readString();
    }
}

destinationstation:

import com.google.gson.annotations.SerializedName;

public class DestinationStation implements Parcelable {
    @SerializedName("crsCode")
 String crsCode;
    @SerializedName("stationName")
 String stationName;
public DestinationStation(String crsCode, String stationName) {
        // TODO Auto-generated constructor stub
    super();
    this.crsCode=crsCode;
    this.stationName=stationName;
    }
public String getCrsCode() {
    return crsCode;
}
public void setCrsCode(String crsCode) {
    this.crsCode = crsCode;
}
public String getStationName() {
    return stationName;
}
public void setStationName(String stationName) {
    this.stationName = stationName;
}
@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(crsCode);
    dest.writeString(stationName);  
}

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

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

private DestinationStation(Parcel in) {

    this.crsCode=in.readString();
    this.stationName=in.readString();
}
}

我在这一行收到错误

dest.writeString(destinationStation);

您能否告诉我如何删除此错误?

0 个答案:

没有答案