android对象序列化

在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;
	}



	@Override
	public 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;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 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对象
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		dest.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;
    	 }
}
嗯,就是这样的

猜你喜欢

转载自blog.csdn.net/qq_26617627/article/details/52932771
今日推荐