猿问

我无法将带有一个片段的包的 ArrayList 放入另一个片段

我想在另一个片段中使用我自己的数据类型的 ArrayList,我用一个包对其进行测试,并将列表与 putParcelableArrayList 方法放在一起,但它没有用。

任何想法为什么,或更好的建议?

片段 1:

Bundle arguments = new Bundle();

arguments.putParcelableArrayList("BESTAND", (ArrayList<Bestand>) palBestand);

片段 2:

ArrayList<Bestand> bestandsliste = (ArrayList<Bestand>) getArguments().getParcelableArrayList("BESTAND");



一只萌萌小番薯
浏览 117回答 1
1回答

www说

这是 Parcelable 类的示例public class Student implements Parcelable{&nbsp; &nbsp; &nbsp; &nbsp; private String id;&nbsp; &nbsp; &nbsp; &nbsp; private String name;&nbsp; &nbsp; &nbsp; &nbsp; private String grade;&nbsp; &nbsp; &nbsp; &nbsp; // Constructor&nbsp; &nbsp; &nbsp; &nbsp; public Student(String id, String name, String grade){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.grade = grade;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;// Getter and setter methods&nbsp; &nbsp; &nbsp; &nbsp;.........&nbsp; &nbsp; &nbsp; &nbsp;.........&nbsp; &nbsp; &nbsp; &nbsp;// Parcelling part&nbsp; &nbsp; &nbsp; &nbsp;public Student(Parcel in){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String[] data = new String[3];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;in.readStringArray(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// the order needs to be the same as in writeToParcel() method&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.id = data[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.name = data[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.grade = data[2];&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;@Оverride&nbsp; &nbsp; &nbsp; &nbsp;public int describeContents(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return 0;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;@Override&nbsp; &nbsp; &nbsp; &nbsp;public void writeToParcel(Parcel dest, int flags) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dest.writeStringArray(new String[] {this.id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.name,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.grade});&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public Student createFromParcel(Parcel in) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return new Student(in);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public Student[] newArray(int size) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return new Student[size];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;};&nbsp; &nbsp;}
随时随地看视频慕课网APP

相关分类

Java
我要回答