Android project combat (19): Android Studio excellent plugin: Parcelable Code Generator Android Studio excellent plugin (2): Parcelable Code Generator

Original text: Android project combat (19): Android Studio excellent plug-in: Parcelable Code Generator

Android Studio excellent plugin series:

                      Android Studio excellent plugin (1): GsonFormat

                      Excellent plugin for Android Studio (2): Parcelable Code Generator

 

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

Parcelable, this word should be familiar to everyone, an interface for serializing objects

If you are not clear, you can look at this blog: Two methods of passing objects in Intent

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

It is assumed here that we have already used Parcelable to serialize an object~~

Then you will find that Parcelable is a bit complicated to use, because we have to rewrite several methods ourselves, and when there are many attributes of the class, we will feel uncomfortable, and we must pay attention not to write the wrong attribute name, and pay attention to write the correct attribute. It takes a lot of time to do repetitive things.

 

So because Parcelable has the advantage of using it, we can't give up, so what should we do?

Android Studio provides us with a plugin to simplify the process of implementing the Parcelable interface for a class.

 

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

Now learn how to use this plugin:

 

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

 

 

 

 

2. Select Plugins, search for Parcel, if you have not downloaded this plugin, "Nothing to show. Click Browse to...." will be displayed under the search box.

 

 

 

 

 

3. Then click Browse in the blue font. At this time, the interface as shown below will appear. We only need to select arcel on the left and click the green button "Install plugin" on the right.

 

 

 

 

4. After completing the above three steps, you can use the Parcelable Code Generator plugin

how to use it,

(1) Create a class file, the class name is customized according to your needs, and add the attributes you need

(2) Shortcut key alt+insert, the following selection box will appear, select Parcelable

 

Then we see the code, is it much faster than writing it manually

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];
        }
    };
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325034912&siteId=291194637