在使用Parcelable对android中数据的序列化操作还是比较有用的,有人做过通过对比Serializable和Parcelable在android中序列化操作对象的速度比对,大概Parcelable相比Serializable要快10倍左右、、、给一个连接可以瞅瞅他们序列化的区别http://greenrobot.me/devpost/android-parcelable-serializable/
下面来总结一下我们基本数据类型、对象、数组、list等做Parcelable的方法,主要是做个总结直接看下code
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | packagecom.suning.mobile.paysdk.pay;importjava.util.ArrayList;importandroid.os.Parcel;importandroid.os.Parcelable;importcom.yaya.test.OrderInfoBean;/** *  * 〈一句话功能简述〉<br> * 〈功能详细描述〉 数据类型序列化 */publicclassParcelableType implementsParcelable {    /** int 类型 */    intage;    /** String 类型 */    String name;    /** boolean 注意该boolean的get和set方法 **/    booleanisGood;    /** boolean 类型 **/    booleancomplete;    /** 数组 **/    privateString[] ids;    /** 对象 [内部已经序列化] **/    privateOrderInfoBean bean;    /** list **/    privateArrayList<orderinfobean> listBeans;    /**     * 默认构造方法     */    publicParcelableType() {        // TODO Auto-generated constructor stub    }    publicParcelableType(Parcel in) {        readFromParcel(in);    }    /***     * 默认实现     */    @Override    publicintdescribeContents() {        // TODO Auto-generated method stub        return0;    }    @Override    publicvoidwriteToParcel(Parcel dest, intflags) {        /** int 写入 **/        dest.writeInt(age);        /** string 写入 **/        dest.writeString(name);        /** boolean 写入 **/        dest.writeInt(isGood ? 1: 0);        /** boolean 写入 **/        dest.writeInt(complete ? 1: 0);        /** 数组 写入 **/        if(ids != null) {            dest.writeInt(ids.length);        } else{            dest.writeInt(0);        }        dest.writeStringArray(ids);        /** 对象 写入 **/        dest.writeParcelable(bean, flags);        /** list 写入 **/        dest.writeList(listBeans);    }    @SuppressWarnings("unchecked")    privatevoidreadFromParcel(Parcel in) {        /** int 读出 */        age = in.readInt();        /** stirng 读出 */        name = in.readString();        /** boolean 读出 */        isGood = (in.readInt() == 1) ? true: false;        /** boolean 读出 */        complete = (in.readInt() == 1) ? true: false;        /** 数组 读出 */        intlength = in.readInt();        ids = newString[length];        in.readStringArray(ids);        /** 对象 读出 */        bean = in.readParcelable(OrderInfoBean.class.getClassLoader());        /** list 读出 */        listBeans = in.readArrayList(OrderInfoBean.class.getClassLoader());    }    publicstaticfinalParcelable.Creator<parcelabletype> CREATOR = newParcelable.Creator<parcelabletype>() {        publicParcelableType createFromParcel(Parcel in) {            returnnewParcelableType(in);        }        publicParcelableType[] newArray(intsize) {            returnnewParcelableType[size];        }    };    publicintgetAge() {        returnage;    }    publicvoidsetAge(intage) {        this.age = age;    }    publicString getName() {        returnname;    }    publicvoidsetName(String name) {        this.name = name;    }    /**     *      * 功能描述: <br>     * 〈功能详细描述〉 fastJson解析时需要格式     */    publicbooleanisIsGood() {        returnisGood;    }    publicvoidsetIsGood(booleanisGood) {        this.isGood = isGood;    }    publicbooleanisComplete() {        returncomplete;    }    publicvoidsetComplete(booleancomplete) {        this.complete = complete;    }    publicString[] getIds() {        returnids;    }    publicvoidsetIds(String[] ids) {        this.ids = ids;    }    publicOrderInfoBean getBean() {        returnbean;    }    publicvoidsetBean(OrderInfoBean bean) {        this.bean = bean;    }    publicArrayList<orderinfobean> getListBeans() {        returnlistBeans;    }    publicvoidsetListBeans(ArrayList<orderinfobean> listBeans) {        this.listBeans = listBeans;    }} | 
原文:http://www.cnblogs.com/Free-Thinker/p/4725998.html