Android Studio excellent plug-in (2): Parcelable Code Generator, androidparcelable

Source: Internet
Author: User

Android Studio excellent plug-in (2): Parcelable Code Generator, androidparcelable

Android Studio excellent plug-in series:

Android Studio excellent plug-in (1): GsonFormat

Android Studio excellent plug-in (2): Parcelable Code Generator

 

-----------------------------------------------------------------------------

Parcelable, this word should be familiar to everyone. It is an interface used to serialize objects.

If you are not clear about it, you can refer to this blog: Two Methods for Intent to pass objects

-----------------------------------------------------------------------------

Assume that we have used Parcelable to serialize an object ~~

We will find that Parcelable is a little complicated to use, because we have to repeat several methods on our own, and when there are many class attributes, we will suffer, pay attention not to write the wrong attribute name, but also to write the type of the attribute, and spend a lot of time doing repetitive tasks.

 

So because Parcelable has the advantage of using it, we cannot give up. What should we do?

Android Studio provides a plug-in to simplify the process of implementing the Parcelable interface for a class.

 

-----------------------------------------------------------------------------

Now I want to learn how to use this plug-in:

 

1. Open a project in Android Studio and click File --> Settings... in the upper left corner to set the project.

 

 

 

 

2. Select plug-ins and search for Parcel. If you have not downloaded this plug-in, "Nothing to show. Click Browse to..." will be displayed in the search box ...."

 

 

 

 

 

3. Click the Browse in the blue font. The page shown in the figure is displayed. Select arcel on the left and click "Install plugin" on the right.

 

 

 

 

4. After completing the preceding three steps, you can use the Parcelable Code Generator plug-in.

How to use it,

(1) create a class file. The class name is customized according to your requirements. Add the required attributes.

(2) shortcut: alt + insert. The following selection box is displayed. Select Parcelable.

 

Then we can see the code, isn't it much faster than manually writing?

public class People implements Parcelable {    private int id;    private String url;    private int width;    private int height;    private int likeCount;    private String description;    private int time;    private int replyCount;    private int floorCount;    private int likeUserCount;    private int age;    private String name;    private String school;    private int type;    private String sax;    private int userid;    @Override    public int describeContents() {        return 0;    }    @Override    public void writeToParcel(Parcel dest, int flags) {        dest.writeInt(this.id);        dest.writeString(this.url);        dest.writeInt(this.width);        dest.writeInt(this.height);        dest.writeInt(this.likeCount);        dest.writeString(this.description);        dest.writeInt(this.time);        dest.writeInt(this.replyCount);        dest.writeInt(this.floorCount);        dest.writeInt(this.likeUserCount);        dest.writeInt(this.age);        dest.writeString(this.name);        dest.writeString(this.school);        dest.writeInt(this.type);        dest.writeString(this.sax);        dest.writeInt(this.userid);    }    public People() {    }    protected People(Parcel in) {        this.id = in.readInt();        this.url = in.readString();        this.width = in.readInt();        this.height = in.readInt();        this.likeCount = in.readInt();        this.description = in.readString();        this.time = in.readInt();        this.replyCount = in.readInt();        this.floorCount = in.readInt();        this.likeUserCount = in.readInt();        this.age = in.readInt();        this.name = in.readString();        this.school = in.readString();        this.type = in.readInt();        this.sax = in.readString();        this.userid = in.readInt();    }    public static final Parcelable.Creator<People> CREATOR = new Parcelable.Creator<People>() {        public People createFromParcel(Parcel source) {            return new People(source);        }        public People[] newArray(int size) {            return new People[size];        }    };}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.