1. 程式人生 > >android實體類的Parcelable

android實體類的Parcelable

在android的Intent中,我可能要傳遞一些複雜的資料型別,如list<T>、或者自己定義的實體類,我們不能像傳遞String、int一樣傳遞它。這時候我們應該把自定義的型別序列化。有兩種方式來實現序列化,一種是實現Serializable介面,一種是實現Parcelable介面。兩者的區別:實現Serializable比較簡單,實體類直接implements Serializable即可;Parcelable介面使用稍微複雜,但是程式碼執行效率更優秀,提高程式碼效能。本篇主要講怎樣實現類的Parcelable

 參考文章:http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html

                    http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2016/0204/3944.html

  Parcelable的原始碼:

public interface Parcelable {
    
    public static final int PARCELABLE_WRITE_RETURN_VALUE = 0x0001;
    
    public static final int CONTENTS_FILE_DESCRIPTOR = 0x0001;
    
    //內容描述方法,可以不看
    public int describeContents();
    
    //相當於將實體類寫入Parcel方法,
    public void writeToParcel(Parcel dest, int flags);
    //讀取介面
    public interface Creator<T> {
        
        public T createFromParcel(Parcel source);
        
        public T[] newArray(int size);
    }
}

Parcelable使用步驟

      1. implements Parcelable 實現Parcelable的方法

      2.重寫writeToParel方法,將實體類的引數寫入

      3.重寫describeContentes方法,預設返回0即可

      4.例項化靜態內部物件CREATOR實現介面Parcelable.Creator

      理解:通過writeToParcel將你的物件對映成Parcel物件,再通過createFromParcel將Parcel物件對映成你的物件,也可以將Parcel看成是一個流,通過writeToParcel把物件寫到流裡面,再通過createFromParcel從流裡讀取物件,只不過這個

過程需要你來實現,因此寫的順序和讀的順序必須一致。

Parcelable示例:

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Created by 4 on 2016/4/6.
 */
public class Student implements Parcelable{
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    //內容描述方法
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
    }

    private static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>(){
        @Override
        public Student createFromParcel(Parcel source) {
            return new Student(source);
        }

        @Override
        public Student[] newArray(int size) {
            return new Student[size];
        }
    };
    
    protected Student(Parcel parcel){
        this.name = parcel.readString();
        this.age = parcel.readInt();
    }
}

在Android Studio中可以快速實現物件的Parcelable化

    1.安裝Android Parcelable code generator外掛

        開啟File ---> Settings ---> Plugins ---> Browse repositories...

        

   

   

   重啟Android studio,顯示下圖,則說明安裝成功

   

   使用外掛Parcelable化物件,右鍵物件空白處,選擇 Generate(生成)---->Parcelable--->OK

   

   生成的程式碼:

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Created by 4 on 2016/4/5.
 */
public class Book implements Parcelable {

    private String bookName;
    private int bookNum;

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public int getBookNum() {
        return bookNum;
    }

    public void setBookNum(int bookNum) {
        this.bookNum = bookNum;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.bookName);
        dest.writeInt(this.bookNum);
    }

    public Book() {
    }

    protected Book(Parcel in) {
        this.bookName = in.readString();
        this.bookNum = in.readInt();
    }

    public static final Parcelable.Creator<Book> CREATOR = new Parcelable.Creator<Book>() {
        @Override
        public Book createFromParcel(Parcel source) {
            return new Book(source);
        }

        @Override
        public Book[] newArray(int size) {
            return new Book[size];
        }
    };
}