Android---Intent传送复杂数据

来源:互联网 发布:英菲克直播软件 编辑:程序博客网 时间:2024/05/22 14:01

1. 实现序列化接口的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()};  

2. 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 CREATOR = new Parcelable.Creator()这三个方法。

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()};  

原文链接这里写链接内容

0 0