12_项目中关于android中Parcelable的使用

1.创建一个类实现Parcelable 接口,重写接口中的方法(并且要求为这个类写一个CREATOR的字段来完成Category的还原)
package com.example.jd.domain;

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


public class Category implements Parcelable {

	private Integer id;
	private String name;
	private String image;

	public Category() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Category(Integer id, String name, String image) {
		super();
		this.id = id;
		this.name = name;
		this.image = image;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	@Override
	public String toString() {
		return "Category [id=" + id + ", name=" + name + ", image=" + image
				+ "]";
	}

	@Override
	public int describeContents() {

		return 0;
	}

	/**
	 * 写入
	 */
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(id);
		dest.writeString(name);
		dest.writeString(image);
	}
        //CREATOR是要求写的Parcelable.Creator<Category>的创造者对象

	public static final Parcelable.Creator<Category> CREATOR = new Parcelable.Creator<Category>() {

		@Override
		public Category createFromParcel(Parcel source) {
			// TODO Auto-generated method stub
			return new Category(source);
		}

		@Override
		public Category[] newArray(int size) {
			// TODO Auto-generated method stub
			return new Category[size];
		}

	};

	// 从Parcel中读取数据
	private Category(Parcel source) {
		id = source.readInt();
		name = source.readString();
		image = source.readString();
	}
}

2.传递值和获取值得方法

2.1存入值

Bundle data = new Bundle();
data.putParcelableArrayList("entities", entities);
2.2获取值

//获取entities对象
ArrayList<Category> entities = bundle.getParcelableArrayList("entities");



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值