簡體   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