Android Parcelable List <string []> </string []>

时间:2014-09-01 08:13:36

标签: java android json parcelable

我有一个JSON,我想将它放入parcelable类,以便我可以将其对象从一个活动传递给另一个。我曾经多次使用过Parcelable。但这一次我对如何使用低于JSON响应创建parcelable类感到困惑。

实际上混淆是List<String[]> String[]的arraylist,我怎么能通过这个。

[
    {
    puzzle_title: "Puzzle",
    grid_size: "8",
    data: [
    [
    "J",
    "J",
    "P",
    "Y",
    "B",
    "T",
    "P",
    "U"
    ],
    [
    "L",
    "W",
    "B",
    "I",
    "Q",
    "M",
    "S",
    "L"
    ],
    [
    "K",
    "Y",
    "Q",
    "L",
    "N",
    "A",
    "W",
    "D"
    ],
    [
    "Y",
    "E",
    "I",
    "G",
    "U",
    "G",
    "H",
    "V"
    ],
    [
    "B",
    "L",
    "A",
    "C",
    "K",
    "E",
    "I",
    "F"
    ],
    [
    "X",
    "L",
    "Q",
    "T",
    "B",
    "N",
    "T",
    "B"
    ],
    [
    "O",
    "O",
    "R",
    "E",
    "D",
    "T",
    "E",
    "W"
    ],
    [
    "H",
    "W",
    "B",
    "K",
    "H",
    "A",
    "F",
    "V"
    ]
    ],
    words: [
    "RED",
    "YELLOW",
    "BLUE",
    "BLACK",
    "WHITE",
    "MAGENTA"
    ]
    },
    {}
    ]

我已经写下了我的可分类课程,如下所示。

    public class PuzzelSetter implements Parcelable {

    @SerializedName("puzzle_title")
    private String title;
    @SerializedName("grid_size")
    private int size;
    private List<String[]> data = new ArrayList<String[]>();
    private List<String> words = new ArrayList<String>();

    public String getTitle() {
        return title;
    }

    public List<String[]> getData() {
        return data;
    }

    public void setData(List<String[]> data) {
        this.data = data;
    }

    public List<String> getWords() {
        return words;
    }

    public void setWords(List<String> words) {
        this.words = words;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public void setData(ArrayList<String[]> data) {
        this.data = data;
    }

    public void setWords(ArrayList<String> words) {
        this.words = words;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(title);
        dest.writeInt(size);
        dest.writeList(data);
        dest.writeStringList(words);
    }

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

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

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

    public PuzzelSetter(Parcel parcel) {
        title = parcel.readString();
        size = parcel.readInt();
        //data = parcel.readStringArray(val);
        parcel.readStringList(words);
    }

}

更新

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(title);
    dest.writeInt(size);        
    dest.writeStringList(words);
    dest.writeInt(data.size()); // write list size
    for (String[] letters : data) { 
        dest.writeStringArray(letters); //write each element
    }
}

public PuzzelSetter(Parcel parcel) {
    title = parcel.readString();
    size = parcel.readInt();        
    //List<String[]> data = new ArrayList<String[]>();
    int size = parcel.readInt(); // read size
    for (int i = 0; i < size; i++) {
        String[] letters = parcel.createStringArray(); // read each element
        data.add(letters);
    }
    parcel.readStringList(words);
}

欢迎任何建议。

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以将所有列表元素分别写入parcel:

dest.writeInt(data.size()); // write list size
for (String[] letters : data) { 
    dest.writeStringArray(letters); //write each element
}

如何阅读:

List<String[]> data = new ArrayList<String[]>();
int size = parcel.readInt(); // read size
for (int i = 0; i < size; i++) {
    String[] letters = parcel.createStringArray(); // read each element
    data.add(letters);
}

<强> UPD:

在你的情况下:

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(title);
    dest.writeInt(size);        
    dest.writeStringList(words);
    dest.writeInt(data.size()); // write list size
    for (String[] letters : data) { 
        dest.writeStringArray(letters); //write each element
    }
}

public PuzzelSetter(Parcel parcel) {
    title = parcel.readString();
    size = parcel.readInt();        
    parcel.readStringList(words);
    int size = parcel.readInt(); // read size
    for (int i = 0; i < size; i++) {
        String[] letters = parcel.createStringArray(); // read each element
        data.add(letters);
    }
}