Parcelable序列化处理

的带有集合带有bigdecimal的处理

定义一个商品信息类,其中包含bigdecimal、string

1.实现parcelable

2.重写describeContents 和writeToParcel的方法

describeContents 的方法默认处理即可为0,

writeToParcel是序列化处理,将数据写入到Parcel对象中,通过Parcel 对象将所有属性进行处理,若是bigdecimal则将写入string字符串

3.实例化静态内部对象CREATOR,其中createFromParcel是反序列化的处理,将writeToParcel方法序列化的数据进行反序列化。

注意:

writeToParcel中和createFromParcel中即调用的protected GoodInfoModel(Parcel in) 中的处理参数的顺序必须相同

上面这些就是序列化的基本流程,对于Android studio 中类实现Parcelable后重写方法,则自动会生成上述处理。

以下重点标出了bigdecimal的处理

public class GoodInfoModel implements Parcelable {
    private BigDecimal total;
    private BigDecimal weight;
    private String goodsno;
    private String barcode;
    private BigDecimal qty;


    protected GoodInfoModel(Parcel in) {
        total = new BigDecimal(in.readString());
        weight = new BigDecimal(in.readString());
        goodsno = in.readString();
        barcode = in.readString();
        qty = new BigDecimal(in.readString());
        
    }

    public static final Creator<GoodInfoModel> CREATOR = new Creator<GoodInfoModel>() {
        @Override
        public GoodInfoModel createFromParcel(Parcel in) {
            return new GoodInfoModel(in);
        }

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

    public BigDecimal getTotal() {
        return total;
    }

    public void setTotal(BigDecimal total) {
        this.total = total;
    }

    public BigDecimal getWeight() {
        return weight;
    }

    public void setWeight(BigDecimal weight) {
        this.weight = weight;
    }

    public String getGoodsno() {
        return goodsno;
    }

    public void setGoodsno(String goodsno) {
        this.goodsno = goodsno;
    }

    public String getBarcode() {
        return barcode;
    }

    public void setBarcode(String barcode) {
        this.barcode = barcode;
    }
    public BigDecimal getQty() {
        return qty;
    }

    public void setQty(BigDecimal qty) {
        this.qty = qty;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(getUnNullString(total + "", "0"));
        dest.writeString(getUnNullString(weight + "", "0"));
        dest.writeString(goodsno);
        dest.writeString(barcode);
        dest.writeString(getUnNullString(qty + "", "0"));
    }

    // 为空判断
    private String getUnNullString(String s, String defaultData) {
        return (s == null || TextUtils.isEmpty(s) || "null".equals(s)) ? defaultData
                : s;
    }

订单类:

public class OrderInfo implements Parcelable {
    private List<GoodInfoModel> goodInfoModels;
    private BigDecimal total;
    private int count;
    private String flow_no;

    public OrderInfo(Parcel in) {
        goodInfoModels = in.createTypedArrayList(GoodInfoModel.CREATOR);
        total = new BigDecimal(in.readString());
        count = in.readInt();
        flow_no = in.readString();
    }

    public static final Creator<OrderInfo> CREATOR = new Creator<OrderInfo>() {
        @Override
        public OrderInfo createFromParcel(Parcel in) {
            return new OrderInfo(in);
        }

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

    public OrderInfo() {

    }

    // 为空判断
    private String getUnNullString(String s, String defaultData) {
        return (s == null || TextUtils.isEmpty(s) || "null".equals(s)) ? defaultData
                : s;
    }

    public List<GoodInfoModel> getGoodInfoModels() {
        return goodInfoModels;
    }

    public void setGoodInfoModels(List<GoodInfoModel> goodInfoModels) {
        this.goodInfoModels = goodInfoModels;
    }

    public BigDecimal getTotal() {
        return total;
    }

    public String getFlow_no() {
        return flow_no;
    }

    public void setFlow_no(String flow_no) {
        this.flow_no = flow_no;
    } 
  
    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeTypedList(goodInfoModels);
        dest.writeString(getUnNullString(total + "", ""));
        dest.writeInt(count);
        dest.writeString(flow_no);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值