在android中读取和写入parcel中自己类型的数组

时间:2015-07-01 13:34:36

标签: android

我想创建对象来写入和读取包裹响应:

{"optionType":["POWER"],"results":[{"type":"POWER","options":[{"value":"130.0 KW"}]},{"type":"POWER","options":[{"value":"130.0 KW"}]},{"type":"POWER","options":[{"value":"110.0 KW"}]},{"type":"POWER","options":[{"value":"130.0 KW"}]}]}

我创建了两个类:

import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;


public class ResultsOptionsCarIdent implements Parcelable {
    private String type;
    private String[] options;

    public ResultsOptionsCarIdent(String type, String[] options) {
        this.type = type;
        this.options = options;

    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String[] getOptions() {
        return options;
    }

    public void setOptions(String[] options) {
        this.options = options;
    }

    public static final Parcelable.Creator<ResultsOptionsCarIdent> CREATOR = new Parcelable.Creator<ResultsOptionsCarIdent>() {
        public ResultsOptionsCarIdent createFromParcel(Parcel source) {
            return new ResultsOptionsCarIdent(source);
        }

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

    private ResultsOptionsCarIdent(Parcel in) {
        this.type = in.readString();
        this.options = in.createStringArray();

    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(type);
        dest.writeStringArray(options);
    }
}

我开始创建第二个,但我发现了如何为ResultsOptionsCarIdent包裹列表:

import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;


public class OptionTypes implements Parcelable {

    private String[] optionTypes;
    private ResultsOptionsCarIdent[] results;

    public OptionTypes(String[] optionTypes, ResultsOptionsCarIdent[] results) {
        this.optionTypes = optionTypes;
        this.results = results;

    }

    public String[] getOptionTypes() {
        return optionTypes;
    }

    public void setOptionTypes(String[] optionTypes) {
        this.optionTypes = optionTypes;
    }

    public ResultsOptionsCarIdent[] getResults() {
        return results;
    }

    public void setResults(ResultsOptionsCarIdent[] results) {
        this.results = results;
    }

    public static final Parcelable.Creator<OptionTypes> CREATOR = new Parcelable.Creator<OptionTypes>() {
        public OptionTypes createFromParcel(Parcel source) {
            return new OptionTypes(source);
        }

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

    private OptionTypes(Parcel in) {
        this.optionTypes = in.createStringArray();
        this.results = //in.createStringArray();

    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeStringArray(type);
        dest.writeStringArray(results);
    }
}

您能帮助我如何撰写和阅读dest.writeStringArray(results);this.results = //in.createStringArray();

1 个答案:

答案 0 :(得分:1)

使用writeTypedArray()和createTypedArray()。

此外,您的选项是模型中的字符串数组,而json中显示的是键值对列表。您应该使用2个数组(一个用于键,一个用于值)或类似地图的Bundle。