Android Studio添加Parcelable序列化小工具(快速提高開發效率)

來源:互聯網
上載者:User

Android Studio添加Parcelable序列化小工具(快速提高開發效率)
Android Studio添加Parcelable序列化小工具(快速提高開發效率)

Android Studio是google專門為開發Android提供的開發工具,在它內部可以直接的添加一些非常好用的開發小工具,這裡就講解怎樣添加這些小工具,並且向大家推薦一個非常有用的對象傳遞時,必須要把對象序列化的介面Parcelable小工具;

 

這裡先介紹下 Android中實現序列化的兩個選擇:一是實現Serializable介面(是JavaSE本身就支援的),一是實現Parcelable介面(是Android特有功能,效率比實現Serializable介面高效,可用於Intent資料傳遞,也可以用於處理序間通訊(IPC))。實現Serializable介面非常簡單,聲明一下就可以了,而實現Parcelable介面稍微複雜一些,但效率更高,推薦用這種方法提高效能。

並且值得注意的是 Android中Intent傳遞對象也對應有兩種方法:一是Bundle.putSerializable(Key,Object),另一種是Bundle.putParcelable(Key,Object)。當然這些Object是有一定的條件的,前者是實現了Serializable介面,而後者是實現了Parcelable介面。

好了介紹了對象序列化之後,就來看看怎樣添加這樣的小工具了:

第一步:點擊設定(Setting)

 

第二步:點擊Plugins,然後點擊2所指的Browse repositories (瀏覽存放庫)

 

第三步: 然後在1所指的輸入框中收索你想要下載的外掛程式小工具,然後2所指的就是下載數量和使用者評分,3所指的就是安裝,點擊3所指的安裝按鈕,就能下載安裝這個外掛程式了。 這裡要給大家講的是,這裡的外掛程式都是全球Android開發人員都能下載的,所以大家看到很多4顆星和5顆星的評分,都是非常值得去研究,沒事的時候大家就可以百度看看是幹什麼的,覺得有意思的話就可以下來研究一下,這裡有很多有趣的小外掛程式,可以協助我們減少很多不必要的代碼,下面給大家推薦的Parcelable小外掛程式就是方便大家對象序列化的。

好了上面介紹了怎樣安裝小外掛程式的方法,我們接下來就為大家介紹怎樣安裝Parcelable小外掛程式。

在 1處中輸入Parcelable,在下面的收索結果 中就有一個 Android Parcelable code generator,沒錯就是它,它評分還是非常高的,接近5顆星,說明效能啊什麼的還是非常值得肯定的,而且有8萬多人下載。那麼我們就點擊下載安裝後,就可以使用了;如

注意,下載安裝後,必須要重啟Android Studio 剛才安裝的小外掛程式才能使用;

接下就教大家怎麼使用;

第一步:要在你傳遞的實體類 中滑鼠右鍵,如 ,點擊 Generate... (或者直接快速鍵 Alt+Insert)

點擊了Generate...之後,就出現了如下菜單介面,點擊Parcelable,就能直接快速的使對象是想Parcelable了。

然後就會彈出一個視窗,讓我們選擇要序列化的屬性,這裡就全選如:

點擊Ok之後,這個對象就實現了Parcelable,並且在後面會自動的產生一些代碼,這是非常方便的。

下面就是點擊了Parcelable,對象就成功序列化了,這樣就省去了我們很多時間,而且對象使用Parcelable介面實現序列化,在activity之間傳遞是非常快速的。

package com.iqtogether.qxueyou.activity;import android.os.Parcel;import android.os.Parcelable;/** * Created by chengguo on 2016/3/15. */public class User implements Parcelable {    private String userId;    private String userName;    private String userSex;    private int userAge;    public String getUserId() {        return userId;    }    public void setUserId(String userId) {        this.userId = userId;    }    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;    }    public String getUserSex() {        return userSex;    }    public void setUserSex(String userSex) {        this.userSex = userSex;    }    public int getUserAge() {        return userAge;    }    public void setUserAge(int userAge) {        this.userAge = userAge;    }    public String getUserHome() {        return userHome;    }    public void setUserHome(String userHome) {        this.userHome = userHome;    }    private String userHome;    @Override    public int describeContents() {        return 0;    }    @Override    public void writeToParcel(Parcel dest, int flags) {        dest.writeString(this.userId);        dest.writeString(this.userName);        dest.writeString(this.userSex);        dest.writeInt(this.userAge);        dest.writeString(this.userHome);    }    public User() {    }    protected User(Parcel in) {        this.userId = in.readString();        this.userName = in.readString();        this.userSex = in.readString();        this.userAge = in.readInt();        this.userHome = in.readString();    }    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {        public User createFromParcel(Parcel source) {            return new User(source);        }        public User[] newArray(int size) {            return new User[size];        }    };}

注意:如果User對象中包含有對象屬性,這個對象屬性它自身也必須要實現Parcelable介面,如,User對象的一個對象屬性,沒有實現Parcelable介面就會出現序列化失敗的提示;

這裡給User設定一個House對象屬性,這裡做個範例

 

然後錯誤提示如下:

 

提示: 要求傳遞一個實現了Parcelable介面的對象。這時候我們去給House對象實現Parcelable介面就Ok了。

如下House對象:

package com.iqtogether.qxueyou.activity;import android.os.Parcel;import android.os.Parcelable;/** * Created by chengguo on 2016/3/15. */public class House implements Parcelable {    private String price;    public String getPrice() {        return price;    }    public void setPrice(String price) {        this.price = price;    }    @Override    public int describeContents() {        return 0;    }    @Override    public void writeToParcel(Parcel dest, int flags) {        dest.writeString(this.price);    }    public House() {    }    protected House(Parcel in) {        this.price = in.readString();    }    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {        public House createFromParcel(Parcel source) {            return new House(source);        }        public House[] newArray(int size) {            return new House[size];        }    };}


接下來就是整個User對象的代碼 如下:

package com.iqtogether.qxueyou.activity;import android.os.Parcel;import android.os.Parcelable;/** * Created by chengguo on 2016/3/15. */public class User implements Parcelable {    private String userId;    private String userName;    private String userSex;    private int userAge;    private House userHouse;    public House getUserHouse() {        return userHouse;    }    public void setUserHouse(House userHouse) {        this.userHouse = userHouse;    }    public String getUserId() {        return userId;    }    public void setUserId(String userId) {        this.userId = userId;    }    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;    }    public String getUserSex() {        return userSex;    }    public void setUserSex(String userSex) {        this.userSex = userSex;    }    public int getUserAge() {        return userAge;    }    public void setUserAge(int userAge) {        this.userAge = userAge;    }    public String getUserHome() {        return userHome;    }    public void setUserHome(String userHome) {        this.userHome = userHome;    }    private String userHome;    public User() {    }    @Override    public int describeContents() {        return 0;    }    @Override    public void writeToParcel(Parcel dest, int flags) {        dest.writeString(this.userId);        dest.writeString(this.userName);        dest.writeString(this.userSex);        dest.writeInt(this.userAge);        dest.writeParcelable(this.userHouse, flags);        dest.writeString(this.userHome);    }    protected User(Parcel in) {        this.userId = in.readString();        this.userName = in.readString();        this.userSex = in.readString();        this.userAge = in.readInt();        this.userHouse = in.readParcelable(House.class.getClassLoader());        this.userHome = in.readString();    }    public static final Creator CREATOR = new Creator() {        public User createFromParcel(Parcel source) {            return new User(source);        }        public User[] newArray(int size) {            return new User[size];        }    };}
 好了這樣就完成了Pacelable序列化小工具的添加和使用,是不是非常方便快捷呢!最後不要忘了,intent在 傳遞對象是使用的是Bundle.putParcelable(Key,Object),而不是Bundle.putSerializable(Key,Object)。在接收對象時使用的是getIntent( ).getParcelableExter( Key),而不是getIntent( ).getSerializableExtera( Key); 祝大家工作愉快。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.