Android中Parcelable序列化總結

來源:互聯網
上載者:User

Android中Parcelable序列化總結

在使用Parcelable對android中資料的序列化操作還是比較有用的,有人做過通過對比Serializable和Parcelable在android中序列化操作對象的速度比對,大概Parcelable相比Serializable要快10倍左右、、、給一個串連可以瞅瞅他們序列化的區別http://greenrobot.me/devpost/android-parcelable-serializable/


下面來總結一下我們基礎資料型別 (Elementary Data Type)、對象、數組、list等做Parcelable的方法,主要是做個總結直接看下code

package com.suning.mobile.paysdk.pay;import java.util.ArrayList;import android.os.Parcel;import android.os.Parcelable;import com.yaya.test.OrderInfoBean;/** *  * 〈一句話功能簡述〉
* 〈功能詳細描述〉 資料類型序列化 */public class ParcelableType implements Parcelable { /** int 類型 */ int age; /** String 類型 */ String name; /** boolean 注意該boolean的get和set方法 **/ boolean isGood; /** boolean 類型 **/ boolean complete; /** 數組 **/ private String[] ids; /** 對象 [內部已經序列化] **/ private OrderInfoBean bean; /** list **/ private ArrayList listBeans; /** * 預設構造方法 */ public ParcelableType() { // TODO Auto-generated constructor stub } public ParcelableType(Parcel in) { readFromParcel(in); } /*** * 預設實現 */ @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel dest, int flags) { /** 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") private void readFromParcel(Parcel in) { /** int 讀出 */ age = in.readInt(); /** stirng 讀出 */ name = in.readString(); /** boolean 讀出 */ isGood = (in.readInt() == 1) ? true : false; /** boolean 讀出 */ complete = (in.readInt() == 1) ? true : false; /** 數組 讀出 */ int length = in.readInt(); ids = new String[length]; in.readStringArray(ids); /** 對象 讀出 */ bean = in.readParcelable(OrderInfoBean.class.getClassLoader()); /** list 讀出 */ listBeans = in.readArrayList(OrderInfoBean.class.getClassLoader()); } public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { public ParcelableType createFromParcel(Parcel in) { return new ParcelableType(in); } public ParcelableType[] newArray(int size) { return new ParcelableType[size]; } }; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } /** * * 功能描述:
* 〈功能詳細描述〉 fastJson解析時需要格式 */ public boolean isIsGood() { return isGood; } public void setIsGood(boolean isGood) { this.isGood = isGood; } public boolean isComplete() { return complete; } public void setComplete(boolean complete) { this.complete = complete; } public String[] getIds() { return ids; } public void setIds(String[] ids) { this.ids = ids; } public OrderInfoBean getBean() { return bean; } public void setBean(OrderInfoBean bean) { this.bean = bean; } public ArrayList getListBeans() { return listBeans; } public void setListBeans(ArrayList listBeans) { this.listBeans = listBeans; }}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.