簡體   English   中英

我無法將帶有捆綁的ArrayList從一個片段放到另一個片段中

[英]I can't put a ArrayList with a bundle from a fragment into another fragment

我想在另一個片段中使用具有我自己的數據類型的ArrayList,我用捆綁包對其進行測試,然后使用putParcelableArrayList方法放置列表,但是它不起作用。

為什么有任何想法,還是更好的建議?

片段1:

Bundle arguments = new Bundle();

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

片段2:

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

這是Parcelable類的示例

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];
           }
       };
   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM