欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

很详细的android序列化过程Parcelable

程序员文章站 2024-03-07 19:53:15
直接上代码:注释都写的很清楚了。 public class entry implements parcelable{ public int userid; p...

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

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

}

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