0

I want to create object to write and read from parcel response:

{"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"}]}]}

I create two classes:

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

and I start create second but I stuck how can parcel list for 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);
    }
}

Can you help me how can I write and read dest.writeStringArray(results); and this.results = //in.createStringArray();

edi233
  • 3,511
  • 13
  • 56
  • 97
  • 1
    [Please check below link will help you] [1]: http://stackoverflow.com/questions/22446359/android-class-parcelable-with-arraylist – shiva kumar Jul 01 '15 at 13:43

1 Answers1

1

Use writeTypedArray() and createTypedArray().

Also your options are an array of Strings in your model while in the json is clearly a list of key-value pairs. You should probably use 2 arrays (one for keys and one for values) or a map-like Bundle instead.

BladeCoder
  • 12,779
  • 3
  • 59
  • 51