猿问

如何从 Intent 迭代自定义对象的 ArrayList 并将它们添加到

我在意图中有一个自定义 FlightData 对象的 ArrayList。我加载意图并将数组列表设置为 null,并且 foreach 循环还强制我使用Objectas 类型。


将 arraylist 保存到意图中:


intent.putParcelableArrayListExtra("FlightDataList", (ArrayList<? extends Parcelable>) flightDataList);

加载意图:


Intent intent = getIntent();

LinearLayout layout_datasheet =  findViewById(R.id.layout_datasheet);

List flightDataList = intent.getParcelableArrayListExtra("FlightDataList");


if (flightDataList == null){

    Log.d("flightDataList_size", "FlightDataList is null"); // this fires

}


assert flightDataList != null;

for (Object data : flightDataList){

    data = (FlightData) data; // items in list are of type FlightData

    TextView tv = new TextView(this);

    tv.setText(data.toString());

    layout_datasheet.addView(tv);

}

我的自定义类的可分割函数(x,y,时间,有 getters-setters):


@Override

    public int describeContents() {

        return 0;

    }


    @Override

    public void writeToParcel(Parcel dest, int flags) {

        dest.writeDouble(x);

        dest.writeDouble(y);

        dest.writeDouble(time);

    }


    public static final Creator<FlightData> CREATOR = new Creator<FlightData>() {

        @Override

        public FlightData createFromParcel(Parcel in) {

            return new FlightData(in);

        }


        @Override

        public FlightData[] newArray(int size) {

            return new FlightData[size];

        }

    };


慕慕森
浏览 128回答 3
3回答

皈依舞

我怀疑您是否在 FlightData 中实现了 Parcable&nbsp;https://medium.com/techmacademy/how-to-implement-and-use-a-parcelable-class-in-android-part-1-28cca73fc2d1

慕沐林林

它应该有效。我在你的例子中唯一缺少的是构造函数。它可以解释null你所得到的。尝试添加这个构造函数FlightData&nbsp; public FlightData(Parcel in) {&nbsp; &nbsp; x = in.readDouble();&nbsp; &nbsp; y = in.readDouble();&nbsp; &nbsp; time&nbsp; = in.readDouble();&nbsp; }

九州编程

您是否尝试创建一个实现 Parcelable 的数据结构?public class flightDataList implements Parcelable{&nbsp; &nbsp; String dataThingyString;&nbsp; &nbsp; int dataThingyInt;&nbsp; &nbsp; public flightDataList(String dataThingyString, int dataThingyInt){&nbsp; &nbsp; &nbsp; &nbsp; this.dataThingyString = dataThingyString;&nbsp; &nbsp; &nbsp; &nbsp; this.dataThingyInt = dataThingyInt;&nbsp; &nbsp; }&nbsp; &nbsp; public flightDataList(Parcle in){&nbsp; &nbsp; &nbsp; &nbsp; this.dataThingyString = in.readString();&nbsp; &nbsp; &nbsp; &nbsp; this.dataThingyInt = in.readInt();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void writeToParcel(Parcel dest, int flags){&nbsp; &nbsp; &nbsp; &nbsp; dest.writeString(dataThingyString);&nbsp; &nbsp; &nbsp; &nbsp; dest.writeInt(dataThingyInt);&nbsp; &nbsp; }&nbsp; &nbsp; public static final Creator<flightDataList> CREATOR = new Creator<flightDataList>(){&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public flightDataList createFromParcel(Parcel source){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new flightDataList(source);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public flightDataList[] newArray(int size){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new flightDataList[size];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void setdataThingyString(String stringData){&nbsp; &nbsp; &nbsp; &nbsp; this.dataThingyString = stringData;&nbsp; &nbsp; }&nbsp; &nbsp; public void setdataThingyInt(int intData){&nbsp; &nbsp; &nbsp; &nbsp; this.dataThingyInt = intData;&nbsp; &nbsp; }&nbsp; &nbsp; public String getdataThingyString(){&nbsp; &nbsp; &nbsp; &nbsp; return dataThingyString;&nbsp; &nbsp; }&nbsp; &nbsp; public int getdataThingyInt(){&nbsp; &nbsp; &nbsp; &nbsp; return dataThingyInt;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int describeContents(){&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; }}

守着星空守着你

1.首先在FlightData对象模型/pojo/class中实现Parceable2.val flightDataList= ArrayList<FlightData>()3.val args = Bundle()4.args.putParcelableArrayList("FlightDataList", flightDataList)5.intent.putExtra(args)然后获取列表val flightDataList = context.getIntent().getExtras().getParcelableArrayList("FlightDataList")
随时随地看视频慕课网APP

相关分类

Java
我要回答