Android Parcelable中枚举的写法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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];
}
};
}

在实体类中写入与读取时

1
2
3
4
5
//写入,writeToParcel方法中
dest.writeParcelable(couponType, flags);

//读取,构造方法中
couponType = in.readParcelable(CouponType.class.getClassLoader());

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :