Java 我能';t将包含一个片段中的束的ArrayList放入另一个片段中
我想在另一个片段中使用带有我自己数据类型的ArrayList,我用一个bundle测试它,并用putParcelableArrayList方法放置列表,但它不起作用 你知道为什么吗,还是有更好的建议 片段1:Java 我能';t将包含一个片段中的束的ArrayList放入另一个片段中,java,android,android-fragments,arraylist,bundle,Java,Android,Android Fragments,Arraylist,Bundle,我想在另一个片段中使用带有我自己数据类型的ArrayList,我用一个bundle测试它,并用putParcelableArrayList方法放置列表,但它不起作用 你知道为什么吗,还是有更好的建议 片段1: Bundle arguments = new Bundle(); arguments.putParcelableArrayList("BESTAND", (ArrayList<Bestand>) palBestand); Bundle参数=新Bundle(); 参数。put
Bundle arguments = new Bundle();
arguments.putParcelableArrayList("BESTAND", (ArrayList<Bestand>) palBestand);
Bundle参数=新Bundle();
参数。putParcelableArrayList(“BESTAND”,(ArrayList)palBestand);
片段2:
ArrayList<Bestand> bestandsliste = (ArrayList<Bestand>) getArguments().getParcelableArrayList("BESTAND");
ArrayList bestandsliste=(ArrayList)getArguments().getParcelableArrayList(“BESTAND”);
下面是Parcelable类的示例
下面是Parcelable类的示例
你需要将你的模态类如Best和你需要将你的模态类如Best和
public class Student implements Parcelable{
private String id;
private String name;
private String grade;
// Constructor
public Student(String id, String name, String grade){
this.id = id;
this.name = name;
this.grade = grade;
}
// Getter and setter methods
.........
.........
// Parcelling part
public Student(Parcel in){
String[] data = new String[3];
in.readStringArray(data);
// the order needs to be the same as in writeToParcel() method
this.id = data[0];
this.name = data[1];
this.grade = data[2];
}
@Оverride
public int describeContents(){
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[] {this.id,
this.name,
this.grade});
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Student createFromParcel(Parcel in) {
return new Student(in);
}
public Student[] newArray(int size) {
return new Student[size];
}
};
}