Android传递对象的两种方法,(Serializable,Parcelable)

来源:互联网 发布:淘宝直通车多少钱 编辑:程序博客网 时间:2024/06/12 18:44

http://blog.csdn.net/xyz_lmn/article/details/5908355

Intent传递对象的方式有两种,一种是Bundle.putSerializable(key,Object);当然,这里的object是实现了Serializable接口的,另一种是Bundle.putPacelable(key,Object);这里的Object则是需要实现parcelable接口
Bundle.putSerializable(key,Object)
     Object实现Serializable接口,需要生成一个SerialVersionUID,这个是有eclipse自动生成的long的id
     
  1. public class Person implements Serializable {  
  2.     private static final long serialVersionUID = -7060210544600464481L;   
  3.     private String name;  
  4.     private int age;  
  5.     public String getName() {  
  6.         return name;  
  7.     }  
  8.     public void setName(String name) {  
  9.         this.name = name;  
  10.     }  
  11.     public int getAge() {  
  12.         return age;  
  13.     }  
  14.     public void setAge(int age) {  
  15.         this.age = age;  
  16.     }  
  17.       
Bundle.putParcelable(key,Object);
     Object实现接口Parcelable接口,要实现两个方法和一个接口,分别是
          describContents();这个方法不用复写,直接返回0就可以了,具体为什么不清楚
          writeToParcel(Parcle desc,int flags),Parcel的意思就是包裹,顾名思义,写到包里面去,参数为要写进去的包,还有一个flags开关,不清楚用来干嘛的,包desc里面提供了写的方法,
               desc.writeString(),desc.writeInt(),写的时候虽然什么都没跟,但是应该是同过bean里面的字段,就是传进去的参数来做标记的  
          public static final Parcelable.create() CREATOR = new Parcelable.create()接口,名字一定要是CREATOR,里面有两个方法要实现,一个是写数组,一个是写对象,,将对象读出来
               返回一个new T[]的数组public Person[] newArray(int size)
               返回一个对象,CreateFromParcel,和writeToParcel对应public Person createFromParcel(Parcel source)  
  1. public class Book implements Parcelable {  
  2.     private String bookName;  
  3.     private String author;  
  4.     private int publishTime;  
  5.       
  6.     public String getBookName() {  
  7.         return bookName;  
  8.     }  
  9.     public void setBookName(String bookName) {  
  10.         this.bookName = bookName;  
  11.     }  
  12.     public String getAuthor() {  
  13.         return author;  
  14.     }  
  15.     public void setAuthor(String author) {  
  16.         this.author = author;  
  17.     }  
  18.     public int getPublishTime() {  
  19.         return publishTime;  
  20.     }  
  21.     public void setPublishTime(int publishTime) {  
  22.         this.publishTime = publishTime;  
  23.     }  
  24.       
  25.     public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>() {  
  26.         public Book createFromParcel(Parcel source) {  
  27.             Book mBook = new Book();  
  28.             mBook.bookName = source.readString();  
  29.             mBook.author = source.readString();  
  30.             mBook.publishTime = source.readInt();  
  31.             return mBook;  
  32.         }  
  33.         public Book[] newArray(int size) {  
  34.             return new Book[size];  
  35.         }  
  36.     };  
  37.       
  38.     public int describeContents() {  
  39.         return 0;  
  40.     }  
  41.     public void writeToParcel(Parcel parcel, int flags) {  
  42.         parcel.writeString(bookName);  
  43.         parcel.writeString(author);  
  44.         parcel.writeInt(publishTime);  
  45.     }  
  46. }

传递数据的方式:
Serializable
     Person p = new Person();
     Intent intent = new Intent(this,Demo.class);
     Bundle mbundle = new Bundle();
     mbundle.putSerializable(key,p);
     intent.putExtirs(mbundle);
     startActivity(intent);
获取
     Person p  = (Person)getIntent().getSerializableExtra(key);这里没有获取bundle是因为intent里面已经封装了Bundle,
-----------------------------------------------------------------------
Parcelable
     Book b = new Book();
     Intent intent = new Intent(this,Demo.class);
     Bundle mbundle = new Bundle();
     mbundle.putParcelable(key,b);
     intent.putExtras(mbundle);
     startActivity(intent);
获取
     Book b = (Book)getIntent().getParcelableExtra(key);

0 0