欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Parcelable序列化处理

程序员文章站 2024-01-04 08:03:16
的带有集合带有bigdecimal的处理定义一个商品信息类,其中包含bigdecimal、string1.实现parcelable2.重写describeContents 和writeToParcel的方法describeContents 的方法默认处理即可为0,writeToParcel是序列化处理,将数据写入到Parcel对象中,通过Parcel 对象将所有属性进行处理,若是bigdecimal则将写入string字符串3.实例化静态内部对象CREATOR,其中createFro...

的带有集合带有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);
    }

本文地址:https://blog.csdn.net/jiuweitianhu_12345/article/details/112240365

相关标签: Android Parcelable

上一篇:

下一篇: