Android使用Parcelable传递对象方法及注意事项

android提供了一种新的类型:Parcel。本类被用作封装数据的容器,封装后的数据可以通过Intent或IPC传递。 除了基本类型以外,只有实现了Parcelable接口的类才能被放入Parcel中。

Intent 可以携带对象来在不同的Activity中传递数据,要传递的数据必须实现三个方法

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])即可。估计本方法是供外部类反序列化本类数组使用。

 

一定要注意:写入parcel和反序列化parcel时顺序一定要相同,不然数据会出错。

 

  1  /**

  2  * ParcelableObject.java

  3  * com.androidtest.parcelable

  4  *

  5  * Function: TODO

  6  *

  7  *   ver     date              author

  8  * ──────────────────────────────────

  9  *            2011-5-11         Leon

 10  *

 11  * Copyright (c) 2011, TNT All Rights Reserved.

 12 */

 13  

 14 package com.androidtest.parcelable;

 15  

 16 import android.os.Parcel;

 17 import android.os.Parcelable;

 18  

 19 /**

 20  * ClassName:ParcelableObject 需要实现三个方法,describeContents()返回0就可以 、 writeToParcel(Parcel parcel, int flag)

 21  * 写入Parcel和public static final Parcelable.Creator

 22 <parcelableobject><span style="font-size: 14px"> CREATOR 供外部类反序列化该类

 23  * 一定要注意顺序后面这个两个函数读和写的顺序要相同,不然数据会出错

 24  * Function: TODO

 25  * Reason:     TODO ADD REASON

 26  *

 27  * @author   Leon

 28  * @version

 29  * @since    Ver 1.1

 30  * @Date     2011-5-11

 31  */

 32 public class ParcelableObject implements Parcelable{

 33     private  String  name  ;

 34     private  String  surName ;

 35     private  int     age ; 

 36  

 37     public String getName() {

 38         return name;

 39     }

 40  

 41     public void setName(String name) {

 42         this.name = name;

 43     }

 44  

 45     public String getSurName() {

 46         return surName;

 47     }

 48  

 49     public void setSurName(String surName) {

 50         this.surName = surName;

 51     }

 52  

 53     public int getAge() {

 54         return age;

 55     }

 56  

 57     public void setAge(int age) {

 58         this.age = age;

 59     }

 60  

 61     @Override

 62     public int describeContents() {

 63  

 64         // TODO Auto-generated method stub

 65         return 0;

 66  

 67     }

 68  

 69     @Override

 70     public void writeToParcel(Parcel dest, int flag) {

 71  

 72         // TODO Auto-generated method stub

 73  

 74         dest.writeInt(age);

 75         dest.writeString(name);

 76         dest.writeString(surName);

 77  

 78     }

 79     public static final Parcelable.Creator CREATOR = new Creator() {

 80  

 81         @Override

 82         public ParcelableObject createFromParcel(Parcel source) {

 83  

 84             // TODO Auto-generated method stub

 85             ParcelableObject parcelableObj = new ParcelableObject();

 86             parcelableObj.age=source.readInt();

 87             parcelableObj.surName=source.readString();

 88             parcelableObj.name = source.readString();

 89             return parcelableObj;

 90  

 91         }

 92  

 93         @Override

 94         public ParcelableObject[] newArray(int size) {

 95  

 96             // TODO Auto-generated method stub

 97             return new ParcelableObject[size];

 98  

 99         } 

100  

101     };

102 }

 

ParcelableActivity1 发送的代码

 1 package com.androidtest.parcelable;

 2  

 3 import android.app.Activity;

 4 import android.content.Intent;

 5 import android.os.Bundle;

 6 import android.view.View;

 7 import android.view.View.OnClickListener;

 8 import android.widget.Button;

 9 import android.widget.LinearLayout;

10 public class ParcelableActivity1 extends Activity {

11  

12     @Override

13     protected void onCreate(Bundle savedInstanceState) {

14         // TODO Auto-generated method stub

15         super.onCreate(savedInstanceState);

16         LinearLayout linearLayout = new LinearLayout(this);

17         Button button = new Button(this);

18         button.setText("Send Parcelable Object");

19         linearLayout.addView(button);

20         button.setOnClickListener(new OnClickListener(){

21             @Override

22             public void onClick(View arg0) {

23                 ParcelableObject passObject = new ParcelableObject();

24                 passObject.setName("Leon Zhang");

25                 passObject.setAge(18);

26                 passObject.setSurName("xxx");

27  

28                 Intent intent = new Intent(ParcelableActivity1.this,ParcelableActivity2.class);

29 //              使用Bundle发送

30 //                Bundle bundle = new Bundle();

31 //                bundle.putParcelable("key", passObject);

32                 intent.putExtra("key", passObject);

33 //                intent.putExtras(bundle);

34                 intent.putExtra("key2", "out of bundle");

35                 startActivity(intent);

36             }

37         });

38        this.setContentView(linearLayout);

39     }

40  

41 }

ParcelableActivity2 接收的代码

 1 package com.androidtest.parcelable;

 2  

 3 import android.app.Activity;

 4 import android.os.Bundle;

 5 import android.util.Log;

 6  

 7 public class ParcelableActivity2 extends Activity {

 8  

 9     private static final String TAG = ParcelableActivity2.class.getSimpleName();

10  

11     @Override

12     protected void onCreate(Bundle savedInstanceState) {

13  

14         // TODO Auto-generated method stub

15         super.onCreate(savedInstanceState);

16         ParcelableObject passObject = this.getIntent()

17                 .getParcelableExtra("key");

18  

19         Log.v(TAG, "Name: " + passObject.getName() + " Surname: "

20                         + passObject.getSurName() + " Age: "

21                         + String.valueOf(passObject.getAge())

22                         + " out of bundle is :"

23                         + this.getIntent().getStringExtra("key2"));

24         Bundle myBundle = this.getIntent().getExtras();

25         Log.v(TAG,"XX: "

26                         + ((ParcelableObject) myBundle.getParcelable("key"))

27                                 .getName()+ " Key2 :"+ myBundle.getString("key2"));

28  

29     }

30  

31 }

 

 

 

 

 

 

 

你可能感兴趣的:(Parcelable)