java - 如何为双ArrayList实现Parcelable?

标签 java android arrays parcelable jsonschema2pojo

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

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

我的问题是我应该在方法中编写什么才能获取列表中的 double 组。

代码:

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

关于java - 如何为双ArrayList实现Parcelable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59153188/

相关文章:

java - ".and"和 "where"代表什么?

java - 我如何从android中的txt文件中读取特定文本

javascript - 删除javascript中每个数组的第一个元素

javascript - 为什么我不能使用 _.map(new Array(n), Math.random) 创建一个随机数组?

java - 批处理 - Natives、Jars 和松散类文件

java - 如何打印一个字符串而另一个字符串继续打印?

java - Android 从自定义网络流播放视频数据?

android - 折叠工具栏布局,工具栏中带有 Logo 、标题、副标题

android - 带有 Python 后端的 Google Endpoints Android

javascript - $.each 中的数组递增