当前位置: 移动技术网 > 移动技术>移动开发>Android > 很详细的android序列化过程Parcelable

很详细的android序列化过程Parcelable

2019年07月24日  | 移动技术网移动技术  | 我要评论

直接上代码:注释都写的很清楚了。

public class entry implements parcelable{
public int userid;
public string username;
public boolean ismale;
public book book;//序列化对象可以嵌套序列化对象,前提是2个类的对象都被序列号过
//几乎所有情况下都返回0,可以不管
@override
public int describecontents() {
return 0;
}
//序列化对象,将对象写到序列号数据结构中
//flags:大多数情况为0
@override
public void writetoparcel(parcel out, int flags) {
out.writeint(userid);
out.writestring(username);
out.writeint(ismale ? 1:0);
out.writeparcelable(book, 0);
// out.writelist(list);也可以序列号list和map,前提是list和map里面的数据都是可序列号的
// out.writemap(map);
}
public entry(int userid,string username,boolean ismale) {
this.userid = userid;
this.username = username;
this.ismale = ismale;
}
//反序列化
public static final parcelable.creator<entry> creator = new creator<entry>() {
//创建指定长度的原始对象数组
@override
public entry[] newarray(int size) {
// todo auto-generated method stub
return new entry[size];
}
//从序列号过后的对象中创建原始对象
@override
public entry createfromparcel(parcel source) {
// todo auto-generated method stub
return new entry(source);
}
};
//从序列号后的对象中创建原始对象
private entry(parcel in){
userid = in.readint();
username = in.readstring();
ismale = in.readint() == 1;
in.readparcelable(thread.currentthread().getcontextclassloader());
}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网