HistoryData_M类,实现了Parcelable接口,同时该类中又有两个List集合,要传递Data对象则其包含的两个List里的对象也需要分别实现Parcelable接口。
package com.xpro.ibusalmoner.test;
import java.util.List;
import android.os.Parcel;
import android.os.Parcelable;
public class HistoryData_M implements Parcelable{
public HistoryData_M(String id, String plateNumber, String hitchLatitude,
String hitchLongitude, String driverName, String driverTel,
String line, int state, String hitchTime, String number) {
super();
this.id = id;
this.plateNumber = plateNumber;
this.hitchLatitude = hitchLatitude;
this.hitchLongitude = hitchLongitude;
this.driverName = driverName;
this.driverTel = driverTel;
this.line = line;
this.state = state;
this.hitchTime = hitchTime;
this.number = number;
}
public HistoryData_M() {
super();
// TODO Auto-generated constructor stub
}
private String id;
private String plateNumber;
private String hitchLatitude;
private String hitchLongitude;
private String driverName;
private String driverTel;
private String line;
private int state;
private List<HistoryImages_driver_M> images_driver;
private String cause;
private List<HistoryImages_trailer_M> images_trailer;
private String hitchTime;
private String number;
public void setId(String id) {
this.id = id;
}
public String getId() {
return this.id;
}
public void setPlateNumber(String plateNumber) {
this.plateNumber = plateNumber;
}
public String getPlateNumber() {
return this.plateNumber;
}
public void setHitchLatitude(String hitchLatitude) {
this.hitchLatitude = hitchLatitude;
}
public String getHitchLatitude() {
return this.hitchLatitude;
}
public void setHitchLongitude(String hitchLongitude) {
this.hitchLongitude = hitchLongitude;
}
public String getHitchLongitude() {
return this.hitchLongitude;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
public String getDriverName() {
return this.driverName;
}
public void setDriverTel(String driverTel) {
this.driverTel = driverTel;
}
public String getDriverTel() {
return this.driverTel;
}
public void setLine(String line) {
this.line = line;
}
public String getLine() {
return this.line;
}
public void setState(int state) {
this.state = state;
}
public int getState() {
return this.state;
}
public void setImages_driver(List<HistoryImages_driver_M> images_driver) {
this.images_driver = images_driver;
}
public List<HistoryImages_driver_M> getImages_driver() {
return this.images_driver;
}
public void setCause(String cause) {
this.cause = cause;
}
public String getCause() {
return this.cause;
}
public void setImages_trailer(List<HistoryImages_trailer_M> images_trailer) {
this.images_trailer = images_trailer;
}
public List<HistoryImages_trailer_M> getImages_trailer() {
return this.images_trailer;
}
public void setHitchTime(String hitchTime) {
this.hitchTime = hitchTime;
}
public String getHitchTime() {
return this.hitchTime;
}
public void setNumber(String number) {
this.number = number;
}
public String getNumber() {
return this.number;
}
@Override
public String toString() {
return "HistoryData_M [id=" + id + ", plateNumber=" + plateNumber
+ ", hitchLatitude=" + hitchLatitude + ", hitchLongitude="
+ hitchLongitude + ", driverName=" + driverName
+ ", driverTel=" + driverTel + ", line=" + line + ", state="
+ state + ", images_driver=" + images_driver + ", cause="
+ cause + ", images_trailer=" + images_trailer + ", hitchTime="
+ hitchTime + ", number=" + number + "]";
}
@SuppressWarnings("unchecked")
public HistoryData_M(Parcel source){
id=source.readString();
plateNumber=source.readString();
hitchLatitude=source.readString();
hitchLongitude=source.readString();
driverName=source.readString();
driverTel=source.readString();
line=source.readString();
state=source.readInt();
images_driver=source.readArrayList(HistoryImages_driver_M.class.getClassLoader());
cause=source.readString();
images_trailer=source.readArrayList(HistoryImages_trailer_M.class.getClassLoader());
hitchTime=source.readString();
number=source.readString();
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(id);
dest.writeString(plateNumber);
dest.writeString(hitchLatitude);
dest.writeString(hitchLongitude);
dest.writeString(driverName);
dest.writeString(driverTel);
dest.writeString(line);
dest.writeInt(state);
dest.writeList(images_driver);
dest.writeString(cause);
dest.writeList(images_trailer);
dest.writeString(hitchTime);
dest.writeString(number);
}
public static final Creator<HistoryData_M> CREATOR = new Creator<HistoryData_M>() {
/**
* 供外部类反序列化本类数组使用
*/
@Override
public HistoryData_M[] newArray(int size) {
return new HistoryData_M[size];
}
/**
* 从Parcel中读取数据
*/
@Override
public HistoryData_M createFromParcel(Parcel source) {
return new HistoryData_M(source);
}
};
}
package com.xpro.ibusalmoner.test; import android.os.Parcel; import android.os.Parcelable;
public class HistoryImages_driver_M implements Parcelable {private String url;public void setUrl(String url) {this.url = url;}public String getUrl() {return this.url;}public HistoryImages_driver_M(String url) {super();this.url = url;}public HistoryImages_driver_M() {super();// TODO Auto-generated constructor stub}public HistoryImages_driver_M(Parcel source) {// TODO Auto-generated constructor stuburl = source.readString();}@Overridepublic int describeContents() {// TODO Auto-generated method stubreturn 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {// TODO Auto-generated method stubdest.writeString(url);}public static final Creator<HistoryImages_driver_M> CREATOR = new Creator<HistoryImages_driver_M>() {/** * 供外部类反序列化本类数组使用 */@Overridepublic HistoryImages_driver_M[] newArray(int size) {return new HistoryImages_driver_M[size];}/** * 从Parcel中读取数据 */@Overridepublic HistoryImages_driver_M createFromParcel(Parcel source) {return new HistoryImages_driver_M(source);}};}/** * 故障车司机上传的故障图片(地址) * @author huangjh * */
HistoryImages_trailer_M类,也实现了Parcelable接口package com.xpro.ibusalmoner.test; import android.os.Parcel; import android.os.Parcelable; import android.os.Parcelable.Creator; /** * 拖车司机上传的故障图片(地址) * @author huangjh * */ public class HistoryImages_trailer_M implements Parcelable { private String url; public void setUrl(String url) { this.url = url; } public String getUrl() { return this.url; } public HistoryImages_trailer_M() { super(); // TODO Auto-generated constructor stub } public HistoryImages_trailer_M(String url) { super(); this.url = url; } @Override public String toString() { return "HistoryImages_trailer_M [url=" + url + "]"; } public HistoryImages_trailer_M(Parcel source) { // TODO Auto-generated constructor stub url = source.readString(); } @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel dest, int flags) { // TODO Auto-generated method stub dest.writeString(url); } public static final Creator<HistoryImages_trailer_M> CREATOR = new Creator<HistoryImages_trailer_M>() { /** * 供外部类反序列化本类数组使用 */ @Override public HistoryImages_trailer_M[] newArray(int size) { return new HistoryImages_trailer_M[size]; } /** * 从Parcel中读取数据 */ @Override public HistoryImages_trailer_M createFromParcel(Parcel source) { return new HistoryImages_trailer_M(source); } }; }
在FirstActivity中使用,onCreate中public void onListener() { listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub HistoryData_M subData = data.get(position); Bundle bundle=new Bundle(); Intent intent = new Intent(HistoryActivity.this, HistoryInfoActivity.class); bundle.putParcelable("HistoryData_M", subData); intent.putExtras(bundle); startActivity(intent); } }); }
在SecondActivity中获得对象private void getFromIntent() { // TODO Auto-generated method stub Intent intent=getIntent(); if(null!=intent){ Bundle bundle = intent.getExtras(); HistoryData_M subData = (HistoryData_M) bundle.get("HistoryData_M"); if (subData != null) { line=subData.getLine(); name=subData.getDriverName(); tel=subData.getDriverTel(); time=subData.getHitchTime(); lat=subData.getHitchLatitude(); lng=subData.getHitchLongitude(); imagesDriverList=subData.getImages_driver(); imagesTrailerList=subData.getImages_trailer(); Log.e("HistoryActivity", "subData"+subData.toString()); } } }