Parcelable使用

下面来总结一下我们基本数据类型、对象、数组、list等做Parcelable的方法,主要是做个总结直接看下code

package com.suning.mobile.paysdk.pay;

import java.util.ArrayList;

import android.os.Parcel;
import android.os.Parcelable;

import com.yaya.test.OrderInfoBean;

/**
 * 
 * 〈一句话功能简述〉

 * 〈功能详细描述〉 数据类型序列化
 */
public class ParcelableType implements Parcelable {
    /** int 类型 */
    int age;
    /** String 类型 */
    String name;
    /** boolean 注意该boolean的get和set方法 **/
    boolean isGood;
    /** boolean 类型 **/
    boolean complete;
    /** 数组 **/
    private String[] ids;
    /** 对象 [内部已经序列化] **/
    private OrderInfoBean bean;
    /** list **/
    private ArrayList listBeans;

    /**
     * 默认构造方法
     */
    public ParcelableType() {
        // TODO Auto-generated constructor stub
    }

    public ParcelableType(Parcel in) {
        readFromParcel(in);
    }

    /***
     * 默认实现
     */
    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        /** int 写入 **/
        dest.writeInt(age);
        /** string 写入 **/
        dest.writeString(name);
        /** boolean 写入 **/
        dest.writeInt(isGood ? 1 : 0);
        /** boolean 写入 **/
        dest.writeInt(complete ? 1 : 0);
        /** 数组 写入 **/
        if (ids != null) {
            dest.writeInt(ids.length);
        } else {
            dest.writeInt(0);
        }
        dest.writeStringArray(ids);
        /** 对象 写入 **/
        dest.writeParcelable(bean, flags);
        /** list 写入 **/
        dest.writeList(listBeans);

    }

    @SuppressWarnings("unchecked")
    private void readFromParcel(Parcel in) {

        /** int 读出 */
        age = in.readInt();
        /** stirng 读出 */
        name = in.readString();
        /** boolean 读出 */
        isGood = (in.readInt() == 1) ? true : false;
        /** boolean 读出 */
        complete = (in.readInt() == 1) ? true : false;
        /** 数组 读出 */
        int length = in.readInt();
        ids = new String[length];
        in.readStringArray(ids);
        /** 对象 读出 */
        bean = in.readParcelable(OrderInfoBean.class.getClassLoader());
        /** list 读出 */
        listBeans = in.readArrayList(OrderInfoBean.class.getClassLoader());

    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public ParcelableType createFromParcel(Parcel in) {
            return new ParcelableType(in);
        }

        public ParcelableType[] newArray(int size) {
            return new ParcelableType[size];
        }
    };

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * 
     * 功能描述: 

     * 〈功能详细描述〉 fastJson解析时需要格式
     */
    public boolean isIsGood() {
        return isGood;
    }

    public void setIsGood(boolean isGood) {
        this.isGood = isGood;
    }

    public boolean isComplete() {
        return complete;
    }

    public void setComplete(boolean complete) {
        this.complete = complete;
    }

    public String[] getIds() {
        return ids;
    }

    public void setIds(String[] ids) {
        this.ids = ids;
    }

    public OrderInfoBean getBean() {
        return bean;
    }

    public void setBean(OrderInfoBean bean) {
        this.bean = bean;
    }

    public ArrayList getListBeans() {
        return listBeans;
    }

    public void setListBeans(ArrayList listBeans) {
        this.listBeans = listBeans;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值