Parcelable序列化接口示例

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

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


阅读更多
个人分类: Android
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭