简体   繁体   English

使用ArrayList的ArrayList时读写宗地

[英]Read and write Parcel when using ArrayList of ArrayList

I'm trying to read and write this ArrayList structured as well: ArrayList<ArrayList<Pair<Float,Float>>> points I've seen that the only way would by using the class inside the external ArrayList, but in this case I haven't it. 我正在尝试读写这种结构化的ArrayList: ArrayList<ArrayList<Pair<Float,Float>>> points我已经看到,唯一的方法是使用外部ArrayList内部的类,但是在这种情况下,我不是吗 This is my full class, how do I can implement it? 这是我的完整课程,我该如何实施?

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Pair;
import java.io.Serializable;
import java.util.ArrayList;

public class Cornice implements Serializable, Parcelable {

    private String number;
    private ArrayList<ArrayList<Pair<Float,Float>>> points;

    public Cornice(String n, ArrayList<ArrayList<Pair<Float,Float>>> p) {
        number = n;
        if (p!=null) points = new ArrayList<>(p);
        else points=new ArrayList<>();
    }

    protected Cornice(Parcel in) {
        number = in.readString();
        points = //MISSING
    }

    public String getNumber () {
        return number;
    }

    public ArrayList<ArrayList<Pair<Float,Float>>> getPoints () {
        return points;
    }

    public static final Creator<Cornice> CREATOR = new Creator<Cornice>() {
        @Override
        public Cornice createFromParcel(Parcel in) {
            return new Cornice(in);
        }

        @Override
        public Cornice[] newArray(int size) {
            return new Cornice[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(number);
        //MISSING
    }
}

Pair is neither Parcelable, nor Serializable, so what you are trying to do won't work. Pair既不可打包,也不可序列化,因此您尝试执行的操作无效。 You will have to change the data structure you use to hold the points to something else. 您将必须更改用于将点保持在其他位置的数据结构。 For example you can convert it something like this: 例如,您可以将其转换为如下形式:

private ArrayList<ArrayList<Float[]>> points;

or even (if you do not want to support null values) to: 甚至(如果您不想支持空值)可以:

private ArrayList<ArrayList<float[]>> points;

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

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