告別手寫parcelable

在eclipse中

用法

  1. 下載該開源項目
  2. 導入到IDE
  3. 參照下載下來的實例編寫json文件來描述目標類
  4. 運行該java工程
  5. 生成的目標類在output文件夾
  6. 拷貝生成的類到需要它的工程(注意:這整個使用過程沒有接下來在android studio中的方法簡單)

在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);
        }
    }
}
發佈了113 篇原創文章 · 獲贊 6 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章