模型类中 Parcelable 接口使用

 1 package com.exmyth.ui.model;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 public class ProductItem{
 7     //-------------------------以下字段在扫描中需要额外用到-------------------------
 8     protected String productId = "";    //产品id,扫描标签时候会返回该字段
 9     protected String brandName = "";
10     //-------------------------以上字段在扫描中需要额外用到-------------------------
11     
12     protected String productName = "";
13     protected String standard = "";
14     protected String featureDesc = "";
15     protected String[] productUrls;
16     protected List<Entry> params = new ArrayList<Entry>();
17 
18     public ProductItem() {
19         super();
20     }
21 
22     public String getProductId() {
23         return productId;
24     }
25     public void setProductId(String productId) {
26         this.productId = productId;
27     }
28     public String getBrandName() {
29         return brandName;
30     }
31     public void setBrandName(String brandName) {
32         this.brandName = brandName;
33     }
34 
35     
36     
37     public String getProductName() {
38         return productName;
39     }
40 
41     public void setProductName(String productName) {
42         this.productName = productName;
43     }
44 
45     public String getStandard() {
46         return standard;
47     }
48 
49     public void setStandard(String standard) {
50         this.standard = standard;
51     }
52 
53     public String getFeatureDesc() {
54         return featureDesc;
55     }
56 
57     public void setFeatureDesc(String featureDesc) {
58         this.featureDesc = featureDesc;
59     }
60 
61     public String[] getProductUrls() {
62         return productUrls;
63     }
64 
65     public void setProductUrls(String[] productUrls) {
66         this.productUrls = productUrls;
67     }
68 
69     public List<Entry> getParams() {
70         return params;
71     }
72 
73     public void setParams(List<Entry> params) {
74         this.params = params;
75     }
76 }
  1 package com.exmyth.ui.model;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 
  6 import android.os.Parcel;
  7 import android.os.Parcelable;
  8 
  9 public class GoodsItem extends ProductItem implements Parcelable{
 10     private String bindId;
 11     private String produceDate = "";
 12 
 13     public GoodsItem() {
 14         super();
 15     }
 16 
 17     public String getBindId() {
 18         return bindId;
 19     }
 20 
 21     public void setBindId(String bindId) {
 22         this.bindId = bindId;
 23     }
 24     
 25     public String getProduceDate() {
 26         return produceDate;
 27     }
 28 
 29     public void setProduceDate(String produceDate) {
 30         this.produceDate = produceDate;
 31     }
 32 
 33     @Override
 34     public int describeContents() {
 35         // TODO Auto-generated method stub
 36         return 0;
 37     }
 38 
 39     @Override
 40     public void writeToParcel(Parcel dest, int flags) {
 41         // TODO Auto-generated method stub
 42         dest.writeString(bindId);
 43         dest.writeString(productId);
 44         dest.writeString(standard);
 45         dest.writeString(productName);
 46         dest.writeString(featureDesc);
 47         dest.writeString(brandName);
 48         dest.writeString(produceDate);
 49         
 50         if(null == productUrls){
 51             dest.writeInt(0);
 52         }else{
 53             dest.writeInt(productUrls.length);
 54             dest.writeStringArray(productUrls);
 55         }
 56         if(null == params || 0 == params.size()){
 57             dest.writeInt(0);
 58         }
 59         else{
 60             dest.writeInt(params.size());
 61 //            dest.writeList(params);
 62             dest.writeTypedList(params);
 63         }
 64     }
 65 
 66     public static final Parcelable.Creator<GoodsItem> CREATOR = new Creator<GoodsItem>() {
 67 
 68         @Override
 69         public GoodsItem createFromParcel(Parcel source) {
 70             GoodsItem goodsItem = new GoodsItem();
 71             goodsItem.bindId = source.readString();
 72             goodsItem.productId = source.readString();
 73             goodsItem.standard = source.readString();
 74             goodsItem.productName = source.readString();
 75             goodsItem.featureDesc = source.readString();
 76             goodsItem.brandName = source.readString();
 77             goodsItem.produceDate = source.readString();
 78             
 79             int length = source.readInt();
 80             String[] productUrls = new String[length];
 81             if(length > 0){
 82                 source.readStringArray(productUrls);
 83             }
 84             goodsItem.productUrls = productUrls;
 85             
 86             int size = source.readInt();
 87             List<Entry> params = new ArrayList<Entry>(size);
 88             if(size > 0){
 89 //                source.readList(params, GoodsItem.class.getClassLoader());
 90                 source.readTypedList(params, Entry.CREATOR);
 91             }
 92             goodsItem.params = params;
 93             
 94             return goodsItem;
 95         }
 96 
 97         @Override
 98         public GoodsItem[] newArray(int size) {
 99             // TODO Auto-generated method stub
100             return new GoodsItem[size];
101         }
102     };
103 }
  1 package com.exmyth.ui.model;
  2 
  3 import android.os.Parcel;
  4 import android.os.Parcelable;
  5 
  6 
  7 /*
  8  featureDesc: 产品特色描述
  9  productName: 产品名称
 10  standard:产品规格
 11  type:
 12  name:参数名称
 13  value:参数值
 14  showOrder:显示顺序
 15  */
 16 public class Entry implements Parcelable{
 17     
 18     public static final String PARAM_TYPE_COMBO = "1";
 19     public static final String PARAM_TYPE_TEXT = "2";
 20     public static final String PARAM_TYPE_CONTACT = "3";
 21     public static final String PARAM_TYPE_HYPERLINK = "4";
 22     private String type; // 参数类型 1-下拉 2-文本 3-联系电话 4-链接地址
 23     private String name = ""; // 参数名称
 24     private String[] value; // 参数值
 25     private String showOrder = "0"; // 显示顺序
 26 //    public ViewType viewType = ViewType.TEXT; // 控件类型
 27     private int index = 0; // 文本,电话,链接地址的类型为0;控件类型为combo的,默认第一项
 28     private boolean isShow = false;
 29 
 30 //    public enum ViewType {
 31 //        COMBO("1"), TEXT("2"), CONTACT("3"), HYPERLINK("4");
 32 //
 33 //        String value;
 34 //
 35 //        private ViewType(String value) {
 36 //            this.value = value;
 37 //        }
 38 //    }
 39 
 40     public Entry() {
 41         super();
 42     }
 43 
 44     public Entry(String type, String name, String[] value, String showOrder) {
 45         this(type, name, value, showOrder, 0);
 46     }
 47 
 48     public Entry(String type, String name, String[] value, String showOrder,
 49             int index) {
 50         super();
 51         this.type = type;
 52         this.name = name;
 53         this.value = value;
 54         this.showOrder = showOrder;
 55         this.index = index;
 56     }
 57 
 58     public String getType() {
 59         return type;
 60     }
 61 
 62     public void setType(String type) {
 63         this.type = type;
 64     }
 65 
 66     public String getName() {
 67         return name;
 68     }
 69 
 70     public void setName(String name) {
 71         this.name = name;
 72     }
 73 
 74     public String[] getValue() {
 75         return value;
 76     }
 77 
 78     public void setValue(String[] value) {
 79         this.value = value;
 80     }
 81 
 82     public String getShowOrder() {
 83         return showOrder;
 84     }
 85 
 86     public void setShowOrder(String showOrder) {
 87         this.showOrder = showOrder;
 88     }
 89 
 90     public int getIndex() {
 91         return index;
 92     }
 93 
 94     public void setIndex(int index) {
 95         this.index = index;
 96     }
 97     
 98     public boolean isShow() {
 99         return isShow;
100     }
101 
102     public void setShow(boolean isShow) {
103         this.isShow = isShow;
104     }
105 
106     @Override
107     public String toString() {
108         return "Entry [type=" + type + ", name=" + name + ", value="
109                 + (null == value ? "" : value[index]) + ", index=" + index + "]";
110     }
111 
112     @Override
113     public int describeContents() {
114         // TODO Auto-generated method stub
115         return 0;
116     }
117 
118     @Override
119     public void writeToParcel(Parcel dest, int flags) {
120         dest.writeString(type);  
121         dest.writeString(name);
122         
123         if(null == value){
124             dest.writeInt(0);
125         }else{
126             dest.writeInt(value.length);
127             dest.writeStringArray(value);
128         }
129         
130         dest.writeString(showOrder);
131         dest.writeInt(index);
132     }
133     
134     public static final Parcelable.Creator<Entry> CREATOR = new Creator<Entry>() {     
135         @Override    
136         public Entry[] newArray(int size) {     
137             return new Entry[size];     
138         }     
139              
140         //将Parcel对象反序列化为UserInfo     
141         @Override    
142         public Entry createFromParcel(Parcel source) {
143             Entry entry = new Entry();
144             entry.type = source.readString();  
145             entry.name = source.readString();
146             
147             
148             int length = source.readInt();
149             String[] value = new String[length];
150             if(length > 0){
151                 source.readStringArray(value);
152             }
153             entry.value = value;
154             
155             entry.showOrder = source.readString();  
156             entry.index = source.readInt();  
157             return entry;     
158         }     
159     };   
160 }
原文地址:https://www.cnblogs.com/exmyth/p/4537081.html