android - 使用Parcelable序列化

1.新的序列化方式:

android提供了一种新的类型:Parcel。本类被用作封装数据的容器,封装后的数据可以通过Intent或IPC传递。

除了基本类型以外,只有实现了Parcelable接口的类才能被放入Parcel中。

Parcelable实现要点:需要实现三个东西

1)writeToParcel 方法。该方法将类的数据写入外部提供的Parcel中.声明如下:

writeToParcel (Parcel dest, int flags) 具体参数含义见javadoc

2)describeContents方法。没搞懂有什么用,反正直接返回0也可以

3)静态的Parcelable.Creator接口,本接口有两个方法:

createFromParcel(Parcel in) 实现从in中创建出类的实例的功能

newArray(int size) 创建一个类型为T,长度为size的数组,仅一句话(return new T[size])即可。估计本方法是供外部类反序列化本类数组使用。

示例:

需求:我们经常需要在多个部件(activity或service)之间通过Intent传递一些数据,简单类型(如数字、字符串)的可以直接放入Intent。复杂类型(例如,J2ee中的Bean)的必须实现Parcelable接口。示例如下:

package cn.wizhy;



import android.os.Parcel;

import android.os.Parcelable;



public class Phone implements Parcelable{

    String type;

    String company;

    int price;

    public Phone(String t,String c,int p) {

        type=t;

        company=c;

        price=p;

    }

    public Phone() {

        // TODO Auto-generated constructor stub

    }

    public String getType() {

        return type;

    }

    public void setType(String type) {

        this.type = type;

    }

    public String getCompany() {

        return company;

    }

    public void setCompany(String company) {

        this.company = company;

    }

    public int getPrice() {

        return price;

    }

    public void setPrice(int price) {

        this.price = price;

    }

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



        @Override

        public Phone createFromParcel(Parcel source) {

            // TODO Auto-generated method stub

            Phone cus = new Phone();  

                cus.type = source.readString();  

                cus.company = source.readString();  

                cus.price = source.readInt();  

                return cus;  

        }



        @Override

        public Phone[] newArray(int size) {

            // TODO Auto-generated method stub

            return new Phone[size];

        }  

        

    };

    @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.writeString(type);

        dest.writeString(company);

        dest.writeInt(price);

        

    }

}

第一个Activity,构造类将其存放到Arraylist里,并通过Intent传给第二个Activity

package cn.wizhy;



import java.util.ArrayList;



import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;



public class Demo extends Activity {

    ArrayList<Phone> info = new ArrayList<Phone>();

    public Phone phone;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        insertPhone();

        Intent intent = new Intent(this,Demo2.class);

//        Bundle bundle = new Bundle();

//        bundle.putSerializable("phone", phone);

       

//        intent.putExtras(bundle);

        phone = new Phone("goole","G1",6000);

        info.add(phone);

        phone = new Phone("apple", "iphone3G", 5000);

        info.add(phone);

       intent.putExtra("phones", info);

        

        startActivity(intent);

    }

    public void insertPhone(){

        phone= new Phone("apple", "iphone3G", 5000);

    }

    

}

第二个Activity接受数据:

package cn.wizhy;



import java.util.ArrayList;



import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;



public class Demo2 extends Activity {

    ArrayList<Phone> info = new ArrayList<Phone>();

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        Intent intent = getIntent();

        info =intent.getParcelableArrayListExtra("phones");

        for(int i=0;i<info.size();i++){

            System.out.println("type="+info.get(i).type+"   company="+info.get(i).company+" price"+info.get(i).price);

        }

    }

}

from:http://aijiawang-126-com.iteye.com/blog/643762

你可能感兴趣的:(Parcelable)