1. 程式人生 > >Android中傳遞物件的三種方法

Android中傳遞物件的三種方法

編輯推薦:稀土掘金,這是一個針對技術開發者的一個應用,你可以在掘金上獲取最新最優質的技術乾貨,不僅僅是Android知識、前端、後端以至於產品和設計都有涉獵,想成為全棧工程師的朋友不要錯過!

Android中,Activity和Fragment之間傳遞物件,可以通過將物件序列化並存入Bundle或者Intent中進行傳遞,也可以將物件轉化為JSON字串,進行傳遞。

序列化物件可以使用Java的Serializable的介面、Parcelable介面。轉化成JSON字串,可以使用Gson等庫。

1.Serializable

Model

  1. publicclass Author implements Serializable{  
  2.     privateint id;  
  3.     private String name;  
  4.     //...
  5. }  
  1. publicclass Book implements Serializable{  
  2.     private String title;  
  3.     private Author author;  
  4.     //...
  5. }  

傳遞資料

  1.   Book book=new Book();   
  2.   book.setTitle("Java程式設計思想");   
  3.   Author author=new Author();   
  4.   author.setId(1);   
  5.   author.setName("Bruce Eckel"
    );   
  6.   book.setAuthor(author);   
  7.   Intent intent=new Intent(this,SecondActivity.class);   
  8.   intent.putExtra("book",book);   
  9.   startActivity(intent);  

接收資料

  1.  Book book= (Book) getIntent().getSerializableExtra("book");  
  2.  Log.d(TAG,"book title->"+book.getTitle());  
  3.  Log.d(TAG,"book author name->"+book.getAuthor().getName());  

2.轉化為JSON字串

Model

  1. publicclass Author{  
  2.     privateint id;  
  3.     private String name;  
  4.     //...
  5. }  
  6. publicclass Book{  
  7.     private String title;  
  8.     private Author author;  
  9.     //...
  10. }  

傳遞資料

  1. Book book=new Book();  
  2. book.setTitle("Java程式設計思想");  
  3. Author author=new Author();  
  4. author.setId(1);  
  5. author.setName("Bruce Eckel");  
  6. book.setAuthor(author);  
  7. Intent intent=new Intent(this,SecondActivity.class);  
  8. intent.putExtra("book",new Gson().toJson(book));  
  9. startActivity(intent);  

接收資料

  1. String bookJson=getIntent().getStringExtra("book");  
  2. Book book=new Gson().fromJson(bookJson,Book.class);  
  3. Log.d(TAG,"book title->"+book.getTitle());  
  4. Log.d(TAG,"book author name->"+book.getAuthor().getName());  

3.使用Parcelable

實現Parcelable介面需要實現兩個方法

  • describeContents方法。內容介面描述,預設返回0就可以;

  • writeToParcel方法。將傳遞的資料打包到Parcel容器中。

除了要實現這兩個方法還必須建立一個Parcelable.Creator介面的例項,用於讀取Parcel容器中的資料

Model

  1. publicclass Author implements Parcelable{  
  2.     privateint id;  
  3.     private String name;  
  4.     //setter & getter...
  5.     @Override  
  6.     publicint describeContents() {  
  7.         return 0;  
  8.     }  
  9.     @Override  
  10.     publicvoid writeToParcel(Parcel dest, int flags) {  
  11.         //該方法將類的資料寫入外部提供的Parcel中.即打包需要傳遞的資料到Parcel容器儲存,
  12.         // 以便從parcel容器獲取資料
  13.         dest.writeString(name);  
  14.         dest.writeInt(id);  
  15.     }  
  16.     publicstaticfinal Creator<Author> CREATOR=new Creator<Author>() {  
  17.         @Override  
  18.         public Author createFromParcel(Parcel source) {  
  19.             //從Parcel容器中讀取傳遞資料值,封裝成Parcelable物件返回邏輯層。
  20.             Author author=new Author();  
  21.             author.setName(source.readString());  
  22.             author.setId(source.readInt());  
  23.             return author;  
  24.         }  
  25.         @Override  
  26.         public Author[] newArray(int size) {  
  27.             //建立一個型別為T,長度為size的陣列,僅一句話(return new T[size])即可。方法是供外部類反序列化本類陣列使用。
  28.             returnnew Author[size];  
  29.         }  
  30.     };  
  31. }  
  1. publicclass Book implements Parcelable{  
  2.     private String title;  
  3.     private Author author;  
  4.     //setter & getter...
  5.     @Override  
  6.     publicint describeContents() {  
  7.         return 0;  
  8.     }  
  9.     @Override  
  10.     publicvoid writeToParcel(Parcel dest, int flags) {  
  11.         dest.writeString(title);  
  12.         dest.writeParcelable(author,flags);  
  13.     }  
  14.     publicstaticfinal Creator<Book> CREATOR=new Creator<Book>() {  
  15.         @Override  
  16.         public Book createFromParcel(Parcel source) {  
  17.             Book book=new Book();  
  18.             book.setTitle(source.readString());  
  19.             book.setAuthor(source.<Author>readParcelable(Author.class.getClassLoader()));  
  20.             return book;