您的位置:首页 > 其它

Intent传递复杂数据的问题

2012-07-15 17:18 330 查看
强制序列化实现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()};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: