androidparcelable序列化成字符串1、
private void test() {
List<String> families = new ArrayList<String>();
families.add("aaa");
families.add("bbb");
String str = object2String(new Student("haha", families));
Student stu = unmarshall(str, Student.CREATOR);
Log.i(TAG, "stu:" + String());
过年作文
恐龙英语怎么说}
private String object2String(Parcelable stu) {
// 1.序列化
Parcel p = Parcel.obtain();
stu.writeToParcel(p, 0);
byte[] bytes = p.marshall();
懊恼近义词
// 2.编码
同学二三事作文String str = deToString(bytes, Ba64.DEFAULT);
return str;
}
public static Parcel unmarshall(byte[] bytes) {
Parcel parcel = Parcel.obtain();
parcel.unmarshall(bytes, 0, bytes.length);
parcel.tDataPosition(0); // this is extremely important!
return parcel;
}
public static <T> T unmarshall(String str, Parcelable.Creator<T> creator) {
// 1.解码
byte[] bytes = Ba64.decode(str, Ba64.DEFAULT);
// 2.反序列化
Parcel parcel = unmarshall(bytes);
ateFromParcel(parcel);
巡展车}
2、
public class Student implements Parcelable {
public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() { public Student createFromParcel(Parcel p) {
return new Student(p);
}
@Override
public Student[] newArray(int size) {
return new Student[size];
}
};
@Override
public int describeContents() {
return 0;
睡前恐怖故事}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeList(families);绿萝能晒太阳吗
}
private String name;
private List<String> families;
public Student() {
}
public Student(String name, List<String> families) {
this.name = name;
this.families = families;
}
public Student(Parcel parcel) {
盛开的花朵name = adString();
families = adArrayList(ClassLoader());// TODO
}
// ... getter tter
}