Android Parcelable中枚举的写法

public enum CouponType implements Parcelable {
    UNKNOWN,
    FIXED,
    PERCENT;

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

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

    public static final Creator<CouponType> CREATOR = new Creator<CouponType>() {
        @Override
        public CouponType createFromParcel(Parcel in) {
            return  CouponType.values()[in.readInt()];
        }

        @Override
        public CouponType[] newArray(int size) {
            return new CouponType[size];
        }
    };
}

在实体类中写入与读取时

//写入,writeToParcel方法中
dest.writeParcelable(couponType, flags);

//读取,构造方法中
couponType = in.readParcelable(CouponType.class.getClassLoader());
上一篇 Android多渠道打包之动态修改App名称,图标,applicationId,版本号,添加资源
下一篇 Android TextView文字倾斜
目录
文章列表
1 Android Cannot call this method while RecyclerView is computing a layout or scrolling
Android Cannot call this method while RecyclerView is computing a layout or scrolling
2
matplotlib之plt.subplots
matplotlib之plt.subplots
3
Java多线程编程七(单例模式)
Java多线程编程七(单例模式)
4
玩转Redis - 京东签到领京豆如何实现
玩转Redis - 京东签到领京豆如何实现
5
Spring Security 之多AuthenticationProvider认证模式实现
Spring Security 之多AuthenticationProvider认证模式实现
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。