D. Joe
D. Joe

Reputation: 81

How to implement Parcelable for double ArrayList?

I got a JSON with an array of doubles and another for Strings. I am trying to create a Parcelable model class with the relevant methods. When the methods are created automaticaly, no line for the doubles' array list is created.

I looked for relevant questions and information, but, surprisingly, couldn't find any. I also added a jar file that was supposed to assist: android-parcelable-intellij-plugin.jar. Don't know what it's supposed to do and couldn't find information on how to use it.

My question is what should I write in the methods in order to get the array of doubles in a List.

The code:

public class Country implements Parcelable {
...
    private List<String> timezones;
    private List<Double> latlng;

    protected Country(Parcel in) {
        timezones = in.createStringArrayList();
        this.latlng = new ArrayList<>(); // from jsonschema2pojo.org
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeStringList(timezones);
        //nothing for the double array
    }

    public List<Double> getLatlng() {
        return latlng;
    }

    public void setLatlng(List<Double> latlng) {
        this.latlng = latlng;
    }

    public List<String> getTimezones() {
        return timezones;
    }

    public void setTimezones(List<String> timezones) {
        this.timezones = timezones;
    }

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

    public static final Creator<Country> CREATOR = new Creator<Country>() {
        @Override
        public Country createFromParcel(Parcel in) {
            return new Country(in);
        }

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

Thanks.

Upvotes: 0

Views: 408

Answers (1)

Shouheng Wang
Shouheng Wang

Reputation: 648

Try this below, it works for me :

public class Country implements Parcelable {

    private List<String> timezones;
    private List<Double> latlng;

    public Country(List<String> timezones, List<Double> latlng) {
        this.timezones = timezones;
        this.latlng = latlng;
    }

    protected Country(Parcel in) {
        timezones = in.createStringArrayList();
        double[] doubleArray = in.createDoubleArray();
        latlng = new ArrayList<>();
        if (doubleArray != null) {
            for (double ele : doubleArray) {
                latlng.add(ele);
            }
        }
    }

    public static final Creator<Country> CREATOR = new Creator<Country>() {
        @Override
        public Country createFromParcel(Parcel in) {
            return new Country(in);
        }

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

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeStringList(timezones);
        double[] doubleArray = new double[latlng == null ? 0 : latlng.size()];
        for (int i=0, len=latlng.size(); i<len; i++) {
            doubleArray[i] = latlng.get(i);
        }
        dest.writeDoubleArray(doubleArray);
    }

    @Override
    public String toString() {
        return "Country{" +
                "timezones=" + timezones +
                ", latlng=" + latlng +
                '}';
    }
}

Upvotes: 1

Related Questions