package com.example.lzl.aty1.bean;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class ParcelableBean implements Parcelable{
private int mIntKey;
private List<String> mStringList;
private Province mBean;
private List<Province> mBeanList = new ArrayList<>();
public ParcelableBean() {}
public ParcelableBean(int intKey, List<String> stringList, Province bean, List<Province> beanList) {
mIntKey = intKey;
mStringList = stringList;
mBean = bean;
mBeanList = beanList;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.mIntKey);
//序列化一个String
dest.writeStringList(this.mStringList);
//序列化对象的时候传入对象和一个flag,这里falg一般都为0,除非标记当前对象需要作为返回值返回,不能立即释放。写入Parcel会同时写入类的相关信息
dest.writeParcelable(this.mBean, 0);
//序列化对象集合有两种方式
//方式1序列化时,会将数据和类的信息都写入到Parcel中,读取的时候需要使用类加载器,效率不高
dest.writeList(this.mBeanList);
//方式2不会写入类的信息,所以读取的时候必须明确的知道是那个类并传入该类的CREATOR来创建对象
dest.writeTypedList(this.mBeanList);
}
private ParcelableBean(Parcel dest) {
this.mIntKey = dest.readInt();
this.mStringList = dest.createStringArrayList();
//读取的时候需要使用类加载器,因为写入的时候同时写入了类的相关信息
this.mBean = dest.readParcelable(Province.class.getClassLoader());
//对应写入方式1
dest.readList(this.mBeanList, Province.class.getClassLoader());
//对应写入方式2
dest.readTypedList(this.mBeanList, Province.CREATOR);
this.mBeanList = dest.createTypedArrayList(Province.CREATOR); //该方法与上一行方法效果一致
} public static final Creator<ParcelableBean> CREATOR = new Creator<ParcelableBean>() { @Override public ParcelableBean createFromParcel(Parcel source) { return new ParcelableBean(source); } @Override public ParcelableBean[] newArray(int size) { return new ParcelableBean[size]; } };
//get set Methods...
}