android对象序列化
来源:互联网 发布:数据结构与算法 树 编辑:程序博客网 时间:2024/05/16 18:59
在java中将对象序列化只需要实现Serializable接口就行了,
相当简单,倘若使用android自带的接口实现序列化貌似不是
那么简单。上网查看了一下,有很多高手写的教程,于是我
就改了自己的项目,也实现了一回,当中碰到一个问题,就是
没有对boolean类型的操作,请教了一下网上的大神,OK解决!
现记录下来,好了上代码:
//修改Serializable为Parcelable 实现对象的序列化public class StudentComputerBean implements Parcelable {private int col;private int row;private int x;private int y;private int seat_num;private String seat_xy;private int seat_group;private String stu_name;private String stu_ip;private boolean stu_usb;private boolean stu_net;public StudentComputerBean() {super();// TODO Auto-generated constructor stub}public StudentComputerBean(int col, int row, int x, int y, int seat_num,String seat_xy, int seat_group, String stu_name, String stu_ip,boolean stu_usb, boolean stu_net) {super();this.col = col;this.row = row;this.x = x;this.y = y;this.seat_num = seat_num;this.seat_xy = seat_xy;this.seat_group = seat_group;this.stu_name = stu_name;this.stu_ip = stu_ip;this.stu_usb = stu_usb;this.stu_net = stu_net;}@Overridepublic String toString() {return "StudentComputerBean [col=" + col + ", row=" + row + ", x=" + x+ ", y=" + y + ", seat_num=" + seat_num + ", seat_xy="+ seat_xy + ", seat_group=" + seat_group + ", stu_name="+ stu_name + ", stu_ip=" + stu_ip + ", stu_usb=" + stu_usb+ ", stu_net=" + stu_net + "]";}public int getCol() {return col;}public void setCol(int col) {this.col = col;}public int getRow() {return row;}public void setRow(int row) {this.row = row;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public int getSeat_num() {return seat_num;}public void setSeat_num(int seat_num) {this.seat_num = seat_num;}public String getSeat_xy() {return seat_xy;}public void setSeat_xy(String seat_xy) {this.seat_xy = seat_xy;}public int getSeat_group() {return seat_group;}public void setSeat_group(int seat_group) {this.seat_group = seat_group;}public String getStu_name() {return stu_name;}public void setStu_name(String stu_name) {this.stu_name = stu_name;}public String getStu_ip() {return stu_ip;}public void setStu_ip(String stu_ip) {this.stu_ip = stu_ip;}public boolean isStu_usb() {return stu_usb;}public void setStu_usb(boolean stu_usb) {this.stu_usb = stu_usb;}public boolean isStu_net() {return stu_net;}public void setStu_net(boolean stu_net) {this.stu_net = stu_net;}@Overridepublic int describeContents() {// TODO Auto-generated method stubreturn 0;}/*private int col;private int row;private int x;private int y;private int seat_num;private String seat_xy;private int seat_group;private String stu_name;private String stu_ip;private boolean stu_usb;private boolean stu_net;*///writeToParcel://dest.writeByte((byte)(myBoolean ?1:0));//if myBoolean == true, byte == 1//readFromParcel://myBoolean =in.readByte()!=0;//myBoolean == true if byte != 0 //实现Parcelable的方法writeToParcel,将ParcelableDate序列化为一个Parcel对象@Overridepublic void writeToParcel(Parcel dest, int flags) {// TODO Auto-generated method stubdest.writeInt(col);dest.writeInt(x);dest.writeInt(y);dest.writeInt(seat_num);dest.writeString(seat_xy);dest.writeInt(seat_group);dest.writeString(stu_name);dest.writeString(stu_ip);dest.writeByte((byte)(stu_usb ?1:0));dest.writeByte((byte)(stu_net ?1:0));} //实例化静态内部对象CREATOR实现接口Parcelable.Creator public static final Parcelable.Creator<StudentComputerBean> CREATOR = new Creator<StudentComputerBean>() { @Override public StudentComputerBean[] newArray(int size) { return new StudentComputerBean[size]; } //将Parcel对象反序列化为ParcelableDate @Override public StudentComputerBean createFromParcel(Parcel in) { return new StudentComputerBean(in); } }; public StudentComputerBean(Parcel in){ //如果元素数据是list类型的时候需要: lits = new ArrayList<?> in.readList(list); //否则会出现空指针异常.并且读出和写入的数据类型必须相同.如果不想对部分关键字进行序列化,可以使用transient关键字来修饰以及static修饰. col = in.readInt(); x = in.readInt(); y = in.readInt(); seat_num = in.readInt(); seat_xy = in.readString(); seat_group = in.readInt(); stu_name = in.readString(); stu_ip = in.readString(); stu_usb =in.readByte()!=0; stu_net =in.readByte()!=0; }}嗯,就是这样的
0 0
- android对象序列化
- android 对象序列化
- android 对象序列化
- Android 对象序列化
- Android对象序列化
- android对象序列化
- Android对象序列化
- Android对象序列化
- Android对象序列化
- Android对象序列化
- Android bitmap对象序列化
- Android Serializalbe对象序列化
- android 序列化对象 Parcelable
- Android对象序列化实现
- Android—对象序列化
- Android 对象序列化 Parcelable
- 读书笔记-Android对象序列化
- Android 中的对象序列化
- 如何用水经注万能地图下载器进行投影转换
- 数据仓库与数据库
- java.lang.ClassNotFoundException: org.apache.struts2.views.gxp.inject.InjectedObjectContainer
- phpStudy启动错误之Apache无法启动
- 数星星
- android对象序列化
- Android RelativeLayout padding 的一个坑?
- Flask开发碰到的socket.error: [Errno 10053]
- 获取控件在屏幕中的坐标
- Android之RecyclerView——用ItemDecoration装饰你的Item
- CentOS 7设置NTP、SSH服务
- python学习笔记 条件判断和循环
- Django Templates的设置
- 解决Cannot change version of project facet Dynamic web module to 2.5