【问题标题】:How to implement Parcelable for double ArrayList?如何为双 ArrayList 实现 Parcelable?
【发布时间】:2020-03-27 21:47:47
【问题描述】:

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

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

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

代码:

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

谢谢。

【问题讨论】:

    标签: java android arrays parcelable jsonschema2pojo


    【解决方案1】:

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

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

    【讨论】:

    • 未使用该答案中的构造函数。 Country Class 使用一个空的构造函数。我试过了,得到了例外:RuntimeException: Unmarshalling unknown type code ...
    • 已添加到问题中。那是你需要的吗?
    猜你喜欢
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 2021-01-13
    • 1970-01-01
    • 2017-07-27
    相关资源
    最近更新 更多