Parcel用来完成数据的序列化传递。下面就介绍一下实现Parcelable接口的方法。
通过实现Parcelable接口序列化对象的步骤:
1、实现Parcelable接口。
2、并且实现Parcelable接口的public
void writeToParcel(Parcel dest, int flags)方法 。
3、自定义类型中必须含有一个名称为CREATOR的静态成员,该成员对象要求实现Parcelable.Creator接口及其方法。
简而言之:通过writeToParcel将你的对象映射成Parcel对象,再通过createFromParcel将Parcel对象映射成你的对象。也可以将Parcel看成是一个流,通过writeToParcel把对象写到流里面,在通过createFromParcel从流里读取对象,只不过这个过程需要你来实现,因此写的顺序和读的顺序必须一致。
package senty.storybaby.entity;
import java.io.Serializable;
import android.R.integer;
import android.os.Parcel;
import android.os.Parcelable;
public class MediaEntity implements Serializable, Parcelable {
public String id = "";
public int categoryId = 0; // 分类ID
public String title = "";
public String tag = ""; // 搜索标签
public String author = "";// 作者
public String album = "";// 专辑
public String format = "";// 格式
public int size = 0;// 尺寸
public int duration = 0;// 时长(单位S)
public int regdate = 0;// 注册时间
public int hits = 0;// 点击数
public int errors = 0; // 错误
public String hasSubTitle = ""; // 是否有字幕
public String subTitle = ""; // 歌词字幕
public String isPay = "";// 是否需要支付,0免费,1需要收费
public float GrowCount = 0; // 成长值
public int playCount = 0; // 播放次数
public int goodCount = 0; // 好评数
public int disCount = 0; // 差评数
public String info = ""; // 介绍
public int downCount = 0; // 下载数
public int favoriteCount = 0; // 收藏数
public String url = "";// 位置
public String BigPic; // 大图
public String MiddlePic; // 中图
public String SmallPic;// 标签小图
public int speed = -1;// 预计速度,分10个等级,只有搜索引擎中的故事有这个属性
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
/**
* 对实体编码
*/
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeInt(categoryId);
dest.writeString(title);
dest.writeString(tag);
dest.writeString(author);
dest.writeString(album);
dest.writeString(format);
dest.writeInt(size);
dest.writeInt(duration);
dest.writeInt(regdate);
dest.writeInt(hits);
dest.writeInt(errors);
dest.writeString(hasSubTitle);
dest.writeString(subTitle);
dest.writeString(isPay);
dest.writeFloat(GrowCount);
dest.writeInt(playCount);
dest.writeInt(goodCount);
dest.writeInt(disCount);
dest.writeString(info);
dest.writeInt(downCount);
dest.writeInt(favoriteCount);
dest.writeString(url);
dest.writeString(BigPic);
dest.writeString(MiddlePic);
dest.writeString(SmallPic);
}
/**
* 解码实体 通过writeToParcel将你的对象映射成Parcel对象
*/
public static final Parcelable.Creator<MediaEntity> CREATOR = new Creator<MediaEntity>() {
public MediaEntity createFromParcel(Parcel source) {
MediaEntity entity = new MediaEntity();
entity.id = source.readString();
entity.categoryId = source.readInt();
entity.title = source.readString();
entity.tag = source.readString();
entity.author = source.readString();
entity.album = source.readString();
entity.format = source.readString();
entity.size = source.readInt();
entity.duration = source.readInt();
entity.regdate = source.readInt();
entity.hits = source.readInt();
entity.errors = source.readInt();
entity.hasSubTitle = source.readString();
entity.subTitle = source.readString();
entity.isPay = source.readString();
entity.GrowCount = source.readFloat();
entity.playCount = source.readInt();
entity.goodCount = source.readInt();
entity.disCount = source.readInt();
entity.info = source.readString();
entity.downCount = source.readInt();
entity.favoriteCount = source.readInt();
entity.url = source.readString();
entity.BigPic = source.readString();
entity.MiddlePic = source.readString();
entity.SmallPic = source.readString();
return entity;
}
public MediaEntity[] newArray(int size) {
// TODO Auto-generated method stub
return new MediaEntity[size];
}
};
}