告別手寫parcelable,手寫parcelable

來源:互聯網
上載者:User

告別手寫parcelable,手寫parcelable
在eclipse中

  • 推薦parcelable code generator
用法在android studio中
  • 推薦安裝外掛程式, android parcelable code generator
用法
  • 書寫自己的目標類
public class DemoParcelable {    String aString;    int aInt;    double aDouble;    HashMap<String, String> aHashMap;    ArrayList<String> aArrayList;    DemoAnotherClass anotherClass;    class DemoAnotherClass{        ConcurrentHashMap<String, String> aConcurrentHashMap;    }}
  • 在類名處alt + insert,在彈出介面選擇Parcelable,最後產生的程式碼如下
public class DemoParcelable implements Parcelable {    public static final Parcelable.Creator<DemoParcelable> CREATOR = new Parcelable.Creator<DemoParcelable>() {        public DemoParcelable createFromParcel(Parcel source) {            return new DemoParcelable(source);        }        public DemoParcelable[] newArray(int size) {            return new DemoParcelable[size];        }    };    String aString;    int aInt;    double aDouble;    HashMap<String, String> aHashMap;    ArrayList<String> aArrayList;    DemoAnotherClass anotherClass;    public DemoParcelable() {    }    protected DemoParcelable(Parcel in) {        this.aString = in.readString();        this.aInt = in.readInt();        this.aDouble = in.readDouble();        this.aHashMap = (HashMap<String, String>) in.readSerializable();        this.aArrayList = in.createStringArrayList();        this.anotherClass = in.readParcelable(DemoAnotherClass.class.getClassLoader());    }    @Override    public int describeContents() {        return 0;    }    @Override    public void writeToParcel(Parcel dest, int flags) {        dest.writeString(this.aString);        dest.writeInt(this.aInt);        dest.writeDouble(this.aDouble);        dest.writeSerializable(this.aHashMap);        dest.writeStringList(this.aArrayList);        dest.writeParcelable(this.anotherClass, flags);    }    static class DemoAnotherClass implements Parcelable {        public static final Creator<DemoAnotherClass> CREATOR = new Creator<DemoAnotherClass>() {            public DemoAnotherClass createFromParcel(Parcel source) {                return new DemoAnotherClass(source);            }            public DemoAnotherClass[] newArray(int size) {                return new DemoAnotherClass[size];            }        };        ConcurrentHashMap<String, String> aConcurrentHashMap;        public DemoAnotherClass() {        }        protected DemoAnotherClass(Parcel in) {            this.aConcurrentHashMap = (ConcurrentHashMap<String, String>) in.readSerializable();        }        @Override        public int describeContents() {            return 0;        }        @Override        public void writeToParcel(Parcel dest, int flags) {            dest.writeSerializable(this.aConcurrentHashMap);        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.