使用String将枚举写入parcel

时间:2014-08-12 11:28:16

标签: java android enums parcelable

我有这样的Parcelable枚举:

 public enum Option implements Parcelable {

    DATA_BASE, TRIPS, BIG_PHOTOS,
    OLD_PHOTOS, FILTERS_IMAGES,
    CATEGORIES_IMAGES, PAGES,
    SOUNDS, PUBLIC_TRANSPORT, MAPS;        

    public static final Parcelable.Creator<Option> CREATOR = new Parcelable.Creator<Option>() {

        public Option createFromParcel(Parcel in) {
            return Option.values()[in.readInt()];
        }

        public Option[] newArray(int size) {
            return new Option[size];
        }

    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(ordinal());
    }
}

现在我修改了它,它看起来像:

 public enum Option implements Parcelable {

    DATA_BASE("Database"), TRIPS("Trips"), BIG_PHOTOS("BigPhotos"),
    OLD_PHOTOS("OldPhotos"), FILTERS_IMAGES("FiltersImages"),
    CATEGORIES_IMAGES("CategoriesImages"), PAGES("Pages"),
    SOUNDS("Sounds"), PUBLIC_TRANSPORT("PublicTransport"), MAPS("Maps");

    private String option;

    Option(String option){
        this.option = option;
    }

    public String getName(){
        return option;
    }

    public static final Parcelable.Creator<Option> CREATOR = new Parcelable.Creator<Option>() {

        public Option createFromParcel(Parcel in) {
            //...
        }

        public Option[] newArray(int size) {
            //...
        }

    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        //...
    }
}

我应该如何在这个枚举中实现writeToParcel(),createFromParcel()和newArray()int?我需要通过额外的意图传递它。

4 个答案:

答案 0 :(得分:34)

这是一个老问题,但有一个更好的解决方案:

dest.writeString(myEnumOption.name());

myEnumOption = MyEnum.valueOf(in.readString());

答案 1 :(得分:4)

我解决了它:

public enum Option implements Parcelable {

    DATA_BASE("Database"), TRIPS("Trips"), BIG_PHOTOS("BigPhotos"),
    OLD_PHOTOS("OldPhotos"), FILTERS_IMAGES("FiltersImages"),
    CATEGORIES_IMAGES("CategoriesImages"), PAGES("Pages"),
    SOUNDS("Sounds"), PUBLIC_TRANSPORT("PublicTransport"), MAPS("Maps");

    private String option;

    Option(String option){
        this.option = option;
    }

    public String getName(){
        return option;
    }

    private void setOption(String option){
        this.option = option;
    }

    public static final Parcelable.Creator<Option> CREATOR = new Parcelable.Creator<Option>() {

        public Option createFromParcel(Parcel in) {
            Option option = Option.values()[in.readInt()];
            option.setOption(in.readString());
            return option;
        }

        public Option[] newArray(int size) {
            return new Option[size];
        }

    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(ordinal());
        out.writeString(option);
    }
}

答案 2 :(得分:2)

OP可能会迟到,但它可能对其他人有所帮助,所以我仍然会发布这个。

  1. 如前所述,将枚举器设置为枚举成员是一种不好的做法。如果您需要该成员,请将其设为最终(这不包括设置者的可能性)

  2. 您无需将该成员(在您的情况下为String'选项')保存到包中,并在重新创建时将其恢复。

  3. 我的实施如下

    public enum Option implements Parcelable {
    
        DATA_BASE("Database"), TRIPS("Trips"), BIG_PHOTOS("BigPhotos"),
        OLD_PHOTOS("OldPhotos"), FILTERS_IMAGES("FiltersImages"),
        CATEGORIES_IMAGES("CategoriesImages"), PAGES("Pages"),
        SOUNDS("Sounds"), PUBLIC_TRANSPORT("PublicTransport"), MAPS("Maps");
    
        private final String option;
    
        Option(String option){
            this.option = option;
        }
    
        public String getName() {
            return option;
        }
    
        public static final Parcelable.Creator<Option> CREATOR = new Parcelable.Creator<Option>() {
    
            public Option createFromParcel(Parcel in) {
                return values()[in.readInt()];
            }
    
            public Option[] newArray(int size) {
                return new Option[size];
            }
    
        };
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel out, int flags) {
            out.writeInt(ordinal());
        }
    
    }
    

答案 3 :(得分:1)

如果您使用枚举Parcelable的唯一原因是将其传递给Intent,则不需要这样做。枚举为Serializable,因此您可以将其传递给:

intent.putExtra("EnumValue", Option.DATA_BASE);

并使用以下方法检索它:

Option myEnum = (Option) intent.getSerializableExtra("EnumValue");