Intent传递复杂数据的问题
2012-07-15 17:18
330 查看
强制序列化实现List传输
先来看不序列化出现的错误提示:
实现序列化接口的Model类
强制序列化代码:
接收数据:
Parcelable实现List传输
Parcelable序列化了的POJO类:
Parcelable序列化Parcelable序列化需要注意的是,需要重载describeContents()、writeToParcel(Parcel dest, int flags)、Parcelable.Creator<Model> CREATOR = new Parcelable.Creator<Model>()这三个方法。
Parcelable数据传递代码:
Parcelable接收数据代码:
先来看不序列化出现的错误提示:
实现序列化接口的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()};
相关文章推荐
- Activity之间使用intent传递大量数据带来问题总结
- Intent之复杂数据的传递
- Activity之间使用intent传递大量数据带来问题总结
- 使用Intent在Activity与Fragment之间传递数据中存在的返回键错乱问题
- Android基础入门教程——4.5.2 Intent之复杂数据的传递
- 05-简单和复杂数据类型存储和传递问题
- Activity之间使用Intent传递大量数据带来问题
- Activity之间使用intent传递大量数据带来问题总结
- Intent之复杂数据的传递
- 4.5.2 Intent之复杂数据的传递
- singleTask模式下Intent传递数据问题
- Activity之间使用intent传递大量数据带来问题总结
- android 之 Intent 传递数据大小有限的问题
- 通信组件之Intent的复杂数据的传递
- Android问题—定义全局数据代替Intent传递数据
- android之Intent复杂数据的传递二(Object类型的数据)
- 关于Intent传递数据的问题(持续更新)
- Intent复杂数据的传递
- Android例子—Intent之复杂数据的传递
- 关于闹钟系统中,intent数据传递出错的问题修复