1.创建一个类实现Parcelable 接口,重写接口中的方法(并且要求为这个类写一个CREATOR的字段来完成Category的还原)
package com.example.jd.domain;
import android.os.Parcel;
import android.os.Parcelable;
public class Category implements Parcelable {
private Integer id;
private String name;
private String image;
public Category() {
super();
// TODO Auto-generated constructor stub
}
public Category(Integer id, String name, String image) {
super();
this.id = id;
this.name = name;
this.image = image;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
@Override
public String toString() {
return "Category [id=" + id + ", name=" + name + ", image=" + image
+ "]";
}
@Override
public int describeContents() {
return 0;
}
/**
* 写入
*/
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(name);
dest.writeString(image);
}
//CREATOR是要求写的Parcelable.Creator<Category>的创造者对象
public static final Parcelable.Creator<Category> CREATOR = new Parcelable.Creator<Category>() {
@Override
public Category createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new Category(source);
}
@Override
public Category[] newArray(int size) {
// TODO Auto-generated method stub
return new Category[size];
}
};
// 从Parcel中读取数据
private Category(Parcel source) {
id = source.readInt();
name = source.readString();
image = source.readString();
}
}
2.传递值和获取值得方法
2.1存入值
Bundle data = new Bundle();
data.putParcelableArrayList("entities", entities);
2.2获取值
//获取entities对象
ArrayList<Category> entities = bundle.getParcelableArrayList("entities");