繁体   English   中英

如何阅读列表<List<List<Double> &gt; 在 Android 中使用 Parcelable

[英]How to read List<List<List<Double>> using Parcelable in Android

在 GET 中检索信息时,我得到了正确的响应。 现在,我正在尝试使用 Post 调用将一些数据发送回服务器。 但是当我这样做时,我得到了classCastException

这是模型类

public class Polygon implements Parcelable
{

    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("coordinates")
    @Expose
    private List<List<List<Double>>> coordinates = new ArrayList<>();
    public final static Creator<Polygon> CREATOR = new Creator<Polygon>() {


        @SuppressWarnings({
                "unchecked"
        })
        public Polygon createFromParcel(android.os.Parcel in) {
            return new Polygon(in);
        }

        public Polygon[] newArray(int size) {
            return (new Polygon[size]);
        }

    };

    protected Polygon(android.os.Parcel in) {
        this.type = ((String) in.readValue((String.class.getClassLoader())));
        in.readList(this.coordinates, (List.class.getClassLoader()));
    }

    public Polygon() {
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<List<List<Double>>> getCoordinates() {
        return coordinates;
    }

    public void setCoordinates(List<List<List<Double>>> coordinates) {
        this.coordinates = coordinates;
    }

    public void writeToParcel(android.os.Parcel dest, int flags) {
        dest.writeValue(type);
        dest.writeList(coordinates);
    }

    public int describeContents() {
        return 0;
    }

}

这是我尝试检索并稍后发回的 json。

{"polygon": {
"type": "Polygon",
"coordinates": [
[
[
-105.18367,
39.54363
],
[
-105.18367,
39.98435
],
[
-104.33773,
39.98435
],
[
-104.33773,
39.54363
],
[
-105.18367,
39.54363
]
     ]
    ]
  }
}

为此,我得到java.lang.ClassCastException: java.lang.Double cannot be cast to java.util.Collection

我尝试将其转换为单独的类以将其用作CREATOR但随后 GET 调用失败。

任何帮助都会很棒!

您可能需要实现一个可Parcelable的特殊ArrayList才能工作。 如何为包含 List<List<String>> 的类实现 Parcelable? 此处接受的答案很好,但您可能希望使用泛型,因此它不仅可以用于String或在您的实例中用于Double

作为旁注,您可能需要查看一个单层解决方案来协调这样的事情。

public class Coordinates implements Parcelable{
    public double x;
    public double y;
    public double z;
    // You get the gist
}
List<Coordinates> coordinates = new ArrayList<>();

如果你能做到这一点,你就可以完全避免上述问题。 同样,我对您的设置一无所知,我可能完全错了,但是嵌套的嵌套List并不是世界上最漂亮的东西。

暂无
暂无

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

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