java 用字符串写入枚举到包裹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25263111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:43:14  来源:igfitidea点击:

Write enum with String to parcel

javaandroidenumsparcelable

提问by Dawid Hy?y

I had Parcelable enum like this:

我有这样的 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());
    }
}

Now I modified it and it looks like:

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

 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) {
        //...
    }
}

How should I implement writeToParcel(), createFromParcel() and newArray() int this Enum? I need that to pass it through extra in intent.

我应该如何在这个 Enum 中实现 writeToParcel()、createFromParcel() 和 newArray()?我需要它来通过额外的意图。

采纳答案by Dawid Hy?y

I solved it by:

我通过以下方式解决了它:

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);
    }
}

回答by Nick Cardoso

This is an old question but there is a better solution:

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

dest.writeString(myEnumOption.name());

and

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

回答by Cosmin Radu

It may be late for the OP, but it may help others, so I'll still post this.

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

  1. As it was mentioned before, it's a bad practice to have a setter to an enum's member. If you need that member, make it final(which excludes the possibility of a setter for it)

  2. You don't need to save that member (in your case the String 'option') into the Parcel and restore it on recreation.

  1. 正如之前提到的,给枚举成员设置一个 setter 是一种不好的做法。如果您需要该成员,请使其成为最终成员(这排除了为其设置 setter 的可能性)

  2. 您不需要将该成员(在您的情况下为 String 'option')保存到 Parcel 中并在娱乐时恢复它。

My implementation would be as follows

我的实现如下

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());
    }

}

回答by HexAndBugs

If the only reason you're making your enum Parcelableis to pass it in an Intent, you don't need to to that. enums are Serializable, so you can pass it like:

如果您进行枚举的唯一原因Parcelable是将其传递给Intent,则您不需要这样做。枚举是Serializable,所以你可以像这样传递它:

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

and retrieve it using:

并使用以下方法检索它:

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