android Parcelable嵌套readParcelable

有时候需要传递复杂对象,用Parcelable代替Serializble更高效一些。下面是一个代码说明,可以作为参考:

public class Message implements Parcelable {

    int id;
    int space_id;
    String text;
    String html;
    String created_at;
    int[] comments;
    boolean is_liked;
    int[] liked_by;
    int[] starred_by;
    boolean is_starred;
    boolean is_followed;
    int[] followed_by;
    ArrayList<AttachmentsData> attachments;
    String url;
    int[] tags;
    boolean is_system;

    public User created_by;
    public Permissions persmissions;
    public Embed embed;
    public Tokens[] tokens;


    /** Parcelable **/
    public Message(Parcel in) {
        readFromParcel(in);
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) { 
        dest.writeInt(id);
        dest.writeInt(space_id);
        dest.writeString(text);
        dest.writeString(html);
        dest.writeString(created_at);
        dest.writeIntArray(comments);
        dest.writeInt(is_liked ? 1 : 0);
        dest.writeIntArray(liked_by);
        dest.writeIntArray(starred_by);
        dest.writeInt(is_starred ? 1 : 0);
        dest.writeInt(is_followed ? 1 : 0);
        dest.writeIntArray(followed_by);
        dest.writeList(attachments);
        dest.writeString(url);
        dest.writeIntArray(tags);
        dest.writeInt(is_system ? 1 : 0);

        dest.writeParcelable(created_by, flags);
        dest.writeParcelable(persmissions, flags);
        dest.writeParcelable(embed, flags);
        
        dest.writeTypedArray(tokens, flags); 
   }

    private void readFromParcel(Parcel in) {
        id = in.readInt();
        space_id = in.readInt();
        text = in.readString();
        html = in.readString();
        created_at = in.readString();
        //in.readIntArray(comments);
        comments = in.createIntArray();
        is_liked = in.readInt() == 0;
        //in.readIntArray(liked_by);
        liked_by = in.createIntArray();
        //in.readIntArray(starred_by);
        starred_by = in.createIntArray();
        is_starred = in.readInt() == 0;
        is_followed = in.readInt() == 0;
        //in.readIntArray(followed_by);
        followed_by = in.createIntArray();
        in.readList(attachments, AttachmentsData.class.getClassLoader());
        url = in.readString();
        //in.readIntArray(tags);
        tags = in.createIntArray();
        is_system = in.readInt() == 0;

        created_by = in.readParcelable(User.class.getClassLoader());
        persmissions = in.readParcelable(Permissions.class.getClassLoader());
        embed = in.readParcelable(Embed.class.getClassLoader());     
        tokens = in.createTypedArray(Tokens.CREATOR);
    }   

    public static final Parcelable.Creator<Message> CREATOR = new Parcelable.Creator<Message>() {

        public Message createFromParcel(Parcel in) {
            return new Message(in);
        }

        public Message[] newArray(int size) {
            return new Message[size];
        }
    };



    /** Getters and setters, I'll spare you these **/

 }

补充:

public class Tokens implements Parcelable {

    String type;
    String value;
    int start;
    int end;


    /** Parcelable related **/
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(type);
        dest.writeString(value);
        dest.writeInt(start);
        dest.writeInt(end);
    }

    private Tokens(Parcel in) {
        type = in.readString();
        value = in.readString();
        start = in.readInt();
        end = in.readInt();
    }

    public static final Parcelable.Creator<Tokens> CREATOR = new Parcelable.Creator<Tokens>() {

        public Tokens createFromParcel(Parcel in) {
            return new Tokens(in);
        }

        public Tokens[] newArray(int size) {
            return new Tokens[size];
        }
    };


    /** Getters and setters **/ 
}

用writeTypedArray和readTypedArray可以读写对象数组到parcel中。

你可能感兴趣的:(android Parcelable嵌套readParcelable)