android中实现Parcelable序列化步骤

时间:2022-09-24 21:20:32
import java.io.Serializable;
import java.text.DecimalFormat; import android.os.Parcel;
import android.os.Parcelable; /**
* 代金券/团购实体类
* @author huqiang
*
*/
public class CouponEntity implements Parcelable{
/**
* shop_id
*/
public String shop_id ;
/**
* 店铺名称
*/
public String shop_name ;
/**
* 代金券id
*/
public String coupon_id ;
/**
* 代金券编号
*/
public String coupon_no ;
/**
* 优惠券标题
*/
public String coupon_name ;
/**
* 优惠券类型 1:代金券 2:团购券
*/
public int coupon_type ; public double getCoupon_sale_amount() {
return coupon_sale_amount;
}
public void setCoupon_sale_amount(double coupon_sale_amount) {
DecimalFormat df = new DecimalFormat("#.00");
this.coupon_sale_amount = Double.parseDouble(df.format(coupon_sale_amount));
}
/**
* 允许2位小数
* @return
*/
public double getCoupon_vaules_amount() {
return coupon_vaules_amount;
}
public void setCoupon_vaules_amount(double coupon_vaules_amount) {
DecimalFormat df = new DecimalFormat("#.00");
this.coupon_vaules_amount = Double.parseDouble(df.format(coupon_vaules_amount));
}
/*---------------------实现Parcelable序列化---------------------------------*/
public CouponEntity(Parcel in)
{
shop_id = in.readString();
shop_name = in.readString();
coupon_id = in.readString();
coupon_no = in.readString();
coupon_type = in.readInt();
}
public static final Parcelable.Creator<CouponEntity> CREATOR = new Parcelable.Creator<CouponEntity>() {
public CouponEntity createFromParcel(Parcel in) {
return new CouponEntity(in);
} public CouponEntity[] newArray(int size) {
return new CouponEntity[size];
}
}; @Override
public int describeContents() {
// 内容描述,返回0即可
return 0;
}
@Override
public void writeToParcel(Parcel out, int arg1) {
out.writeString(shop_id);
out.writeString(shop_name);
out.writeString(coupon_id);
out.writeString(coupon_no);
out.writeInt(coupon_type);
} }

实现Parcelable序列化步骤
1、implements implements Parcelable
2、

    /*---------------------实现Parcelable序列化---------------------------------*/
public CouponEntity(Parcel in)
{
shop_id = in.readString();
shop_name = in.readString();
coupon_id = in.readString();
coupon_no = in.readString();
coupon_type = in.readInt();
}
public static final Parcelable.Creator<CouponEntity> CREATOR = new Parcelable.Creator<CouponEntity>() {
public CouponEntity createFromParcel(Parcel in) {
return new CouponEntity(in);
} public CouponEntity[] newArray(int size) {
return new CouponEntity[size];
}
}; @Override
public int describeContents() {
// 内容描述,返回0即可
return 0;
}
@Override
public void writeToParcel(Parcel out, int arg1) {
out.writeString(shop_id);
out.writeString(shop_name);
out.writeString(coupon_id);
out.writeString(coupon_no);
out.writeInt(coupon_type);
}