关闭

Parcelable序列化接口示例

132人阅读 评论(0) 收藏 举报
分类:

package com.daima.bean;



import android.os.Parcel;
import android.os.Parcelable;


public class UserInfo implements Parcelable {
private String userCode;
private String userName;
private String token;
private boolean canLogin;
private String errInfo;
private String userId;


public UserInfo() {


}


public UserInfo(Parcel in) {
readFromParcel(in);
}


private void readFromParcel(Parcel in) {
// TODO Auto-generated method stub
userCode = in.readString();
userName = in.readString();
token = in.readString();
canLogin = (in.readInt() == 0) ? true : false;
errInfo = in.readString();
userId = in.readString();
}


@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}


@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(userCode);
dest.writeString(userName);
dest.writeString(token);
dest.writeInt(canLogin ? 0 : 1);
dest.writeString(errInfo);
dest.writeString(userId);
}


public static final Parcelable.Creator<UserInfo> CREATOR = new Parcelable.Creator<UserInfo>() {
public UserInfo createFromParcel(Parcel in) {
return new UserInfo(in);
}


public UserInfo[] newArray(int size) {
return new UserInfo[size];
}
};


public String getUserId() {
return userId;
}


public void setUserId(String userId) {
this.userId = userId;
}


public String getErrInfo() {
return errInfo;
}


public void setErrInfo(String errInfo) {
this.errInfo = errInfo;
}


public String getUserCode() {
return userCode;
}


public void setUserCode(String userCode) {
this.userCode = userCode;
}


public String getUserName() {
return userName;
}


public void setUserName(String userName) {
this.userName = userName;
}


public String getToken() {
return token;
}


public void setToken(String token) {
this.token = token;
}


public boolean isCanLogin() {
return canLogin;
}


public void setCanLogin(boolean canLogin) {
this.canLogin = canLogin;
}


}


需要注意的是:

private void readFromParcel(Parcel in) {
// TODO Auto-generated method stub
userCode = in.readString();
userName = in.readString();
token = in.readString();
canLogin = (in.readInt() == 0) ? true : false;
errInfo = in.readString();
userId = in.readString();
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(userCode);
dest.writeString(userName);
dest.writeString(token);
dest.writeInt(canLogin ? 0 : 1);
dest.writeString(errInfo);
dest.writeString(userId);
}

这俩块读写字段的顺序要一致。


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2125次
    • 积分:63
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档