1

I have the following models:

GitItem:

    public class GitItem implements Serializable, Parcelable {
        public int id;
        public String name;
        public String full_name;
        public GitOwner owner;

        public GitItem() {
        }

        public GitItem(Parcel in) {
            id = in.readInt();
            name = in.readString();
            full_name = in.readString();
        }

        public int describeContents() {
            return 0;
        }

        public void writeToParcel(Parcel out, int flags){
            out.writeInt(id);
            out.writeString(name);
            out.writeString(full_name);
        }

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

            @Override
            public GitItem createFromParcel(Parcel source) {
                return new GitItem(source);
            }

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

and GitOwner:

public class GitOwner implements Serializable, Parcelable {
    public String avatar_url;

    public GitOwner(Parcel in) {
        avatar_url = in.readString();
    }

    public GitOwner() {
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {

    }

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

        @Override
        public GitOwner createFromParcel(Parcel source) {
            return  new GitOwner(source);
        }

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

and then in MainActivity.java I have a List<GitItem> data; which is bound to my RecyclerView.

Basically what I am trying to achieve, is to store current state of my List<GitItem> when I change Activity or simply rotate device. Unfortunately, even though I have implemented Parcelable (as suggested by other posts), I was unable to store the instance.

Any help in this matter would be highly appreciated.


You have not implemented the complete body of Parcelable functions i.e writeToParcel in GitOwner and GitItem class

In GitOwner class

@Override
public void writeToParcel(Parcel dest, int flags) {
     dest.writeString(avatar_url);
     // add this line
}

and inside GitItem use writeParcelable to write and readParcelable to read GitItem object otherwise it won't be persisted and restored

    public GitItem(Parcel in) {
        id = in.readInt();
        name = in.readString();
        full_name = in.readString();
        owner = in.readParcelable(GitItem.class.getClassLoader());
        // Add line to read owner object
    }

    public void writeToParcel(Parcel out, int flags){
        out.writeInt(id);
        out.writeString(name);
        out.writeString(full_name);
        out.writeParcelable(owner , flags);           
        // Add line to write owner object
    }
4

1 回答 1

2

您还没有实现完整的Parcelable函数体,即writeToParcelinGitOwnerGitItemclass

GitOwner课堂上

@Override
public void writeToParcel(Parcel dest, int flags) {
     dest.writeString(avatar_url);
     // add this line
}

并在内部GitItem用于writeParcelable写入和readParcelable读取GitItem对象,否则它将不会被持久化和恢复

    public GitItem(Parcel in) {
        id = in.readInt();
        name = in.readString();
        full_name = in.readString();
        owner = in.readParcelable(GitItem.class.getClassLoader());
        // Add line to read owner object
    }

    public void writeToParcel(Parcel out, int flags){
        out.writeInt(id);
        out.writeString(name);
        out.writeString(full_name);
        out.writeParcelable(owner , flags);           
        // Add line to write owner object
    }
于 2017-06-04T11:53:10.897 回答