Intent传递复杂数据的问题

强制序列化实现List传输

 

先来看不序列化出现的错误提示:

实现序列化接口的Model类

/**
 * 
 */
package com.aaron.util;

import java.io.Serializable;

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

/**
 * @author aaron
 *
 */
public class Model implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6680457902587956425L;
	private int month;
	private float total;
	private String store;
	
	
	/**
	 * @return the month
	 */
	public int getMonth() {
		return month;
	}
	/**
	 * @param month the month to set
	 */
	public void setMonth(int month) {
		this.month = month;
	}
	/**
	 * @return the total
	 */
	public float getTotal() {
		return total;
	}
	/**
	 * @param total the total to set
	 */
	public void setTotal(float total) {
		this.total = total;
	}
	/**
	 * @return the store
	 */
	public String getStore() {
		return store;
	}
	/**
	 * @param store the store to set
	 */
	public void setStore(String store) {
		this.store = store;
	}
	
	

}

强制序列化代码:

		//通过Intent传送数据
		Intent intent = new Intent();
		intent.putExtra("Model", (Serializable)list);
		
		intent.setClass(NetClientDemoActivity.this, HelloAchartengineActivity.class);
		NetClientDemoActivity.this.startActivity(intent);

 

接收数据:

			//获取Intent传送过来的数据
			Intent intent = getIntent();
			List<Model> list = (List<Model>) intent.getSerializableExtra("Model");
			
			String[] titles = new String[]{list.get(0).getStore()};


 

 

 

Parcelable实现List传输

 

Parcelable序列化了的POJO类:

/**
 * 
 */
package com.aaron.util;

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

/**
 * @author aaron
 * 
 */
public class Model implements Parcelable {

	private int month;
	private float total;
	private String store;

	/**
	 * @return the month
	 */
	public int getMonth() {
		return month;
	}

	/**
	 * @param month
	 *            the month to set
	 */
	public void setMonth(int month) {
		this.month = month;
	}

	/**
	 * @return the total
	 */
	public float getTotal() {
		return total;
	}

	/**
	 * @param total
	 *            the total to set
	 */
	public void setTotal(float total) {
		this.total = total;
	}

	/**
	 * @return the store
	 */
	public String getStore() {
		return store;
	}

	/**
	 * @param store
	 *            the store to set
	 */
	public void setStore(String store) {
		this.store = store;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		dest.writeInt(month);
		dest.writeString(store);
		dest.writeFloat(total);

	}

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

		@Override
		public Model createFromParcel(Parcel source) {
			// TODO Auto-generated method stub
			Model model = new Model();
			model.month = source.readInt();
			model.store = source.readString();
			model.total = source.readFloat();
			return model;
		}

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


Parcelable序列化Parcelable序列化需要注意的是,需要重载describeContents()、writeToParcel(Parcel dest, int flags)、Parcelable.Creator<Model> CREATOR = new Parcelable.Creator<Model>()这三个方法。

 

Parcelable数据传递代码:

//Parcelable实现序列化传送数据
		Intent intent = new Intent();
		intent.putParcelableArrayListExtra("Model", (ArrayList<? extends Parcelable>) list);
		
		intent.setClass(NetClientDemoActivity.this, HelloAchartengineActivity.class);

 

Parcelable接收数据代码:

	//通过Parcelable反序列化获取数据
			Intent intent = getIntent();
			List<Model> list = intent.getParcelableArrayListExtra("Model");
			
			String[] titles = new String[]{list.get(0).getStore()};



 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值