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

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/26/android-parcelable-write-enumeration/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Android Parcelable中枚举的写法
public enum CouponType implements Parcelable { UNKNOWN, FIXED, PERCENT; @Override public int describeContents() { retur……
<<上一篇
下一篇>>
文章目录
关闭
目 录