繁体   English   中英

如何为双 ArrayList 实现 Parcelable?

[英]How to implement Parcelable for double ArrayList?

我得到了一个带有双精度数组的 JSON 和另一个用于字符串的 JSON。 我正在尝试使用相关方法创建 Parcelable 模型类。 自动创建方法时,不会创建双打数组列表的行。

我寻找相关的问题和信息,但令人惊讶的是,找不到任何相关信息。 我还添加了一个应该提供帮助的 jar 文件:android-parcelable-intellij-plugin.jar。 不知道它应该做什么,也找不到有关如何使用它的信息。

我的问题是我应该在方法中写什么才能获得列表中的双精度数组。

编码:

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];
        }
    };
}
...

谢谢。

在下面试试这个,它对我有用:

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 +
                '}';
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM