2013-03-21 14 views
17
public class Category implements Parcelable { 

    private int mCategoryId; 
    private List<Video> mCategoryVideos; 

public int getCategoryId() { 
     return mCategoryId; 
    } 

    public void setCategoryId(int mCategoryId) { 
     this.mCategoryId = mCategoryId; 
    } 

public List<Video> getCategoryVideos() { 
     return mCategoryVideos; 
    } 

    public void setCategoryVideos(List<Video> videoList) { 
     mCategoryVideos = videoList; 
    } 

@Override 
    public void writeToParcel(Parcel parcel, int i) { 
     parcel.writeInt(mCategoryId); 
     parcel.writeTypedList(mCategoryVideos); 
    } 

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 
     public Category createFromParcel(Parcel parcel) { 
      final Category category = new Category(); 

      category.setCategoryId(parcel.readInt()); 
      category.setCategoryVideos(parcel.readTypedList()); */// **WHAT SHOULD I WRITE HERE*** 

      return category; 
     } 

     public Category[] newArray(int size) { 
      return new Category[size]; 
     } 
    }; 

} 

Im il mio codice che sto usando il modello che implementa da Parcelable ... Qualcuno potrebbe dire che cosa em grido che scrivo in questa linea category.setCategoryVideos(parcel.readTypedList()) non ho potuto trovare alcun posto disponibile.Come scrivere List <> in pacco

MODIFICA: category.setCategoryVideos(parcel.readTypedList(mCategoryVideos,Video.CREATOR)); qui mCategoryVideos Non riesco a risolvere l'errore.

risposta

23

Esistono metodi delle liste per la classe Parcelable, si può dare un'occhiata a loro qui:

readList (List outVal, ClassLoader loader)

writeList (List val)

Nel tuo caso sarebbe simile:

List<Object> myList = new ArrayList<>(); 

parcel.readList(myList,List.class.getClassLoader()); 
category.setCategoryVideos(myList); 
+1

Questa risposta deve essere contrassegnato come corretta, una cosa tho è che la lista deve contenere oggetti Parcelable pure. –

+3

Correzione molto piccola: 'parcel.readList (myList, ElementType.class.getClassLoader());' – WindRider

+1

NullPointerException –

2
public static final Parcelable.Creator<Category> CREATOR = new Parcelable.Creator<Category>() { 
      public Category createFromParcel(Parcel in) { 
       return new Category(in); 
      } 

      public Category[] newArray(int size) { 
       return new Category[size]; 
      } 
     }; 

private Category(Parcel in) { 
    String[] data = new String[1]; 
    in.readStringArray(data); 
    mCategoryId = Integer.parseInt(data[0]); 
} 

public void writeToParcel(Parcel dest, int flags) { 
    dest.writeStringArray(new String[]{ 
      mCategoryId 
    }); 
} 

Quindi nell'attività.

public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putParcelableArrayList("mCategoryVideos", (List<? extends Parcelable>) mCategoryVideos); 
} 

public void onRestoreInstanceState(Bundle inState) { 
    super.onRestoreInstanceState(inState); 
    if (inState != null) { 
     mCategoryVideos = inState.getParcelableArrayList("mCategoryVideos"); 
     // Restore All Necessary Variables Here 
    } 
} 
2

From a good answer:

semplici passaggi:

private List<MyParcelableClass> mList; 

protected MyClassWithInnerList(Parcel in) { 
    mList = in.readArrayList(MyParcelableClass.class.getClassLoader()); 
} 
@Override 
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeList(mList); 
} 
+1

Quando si legge la lista da parcelable è meglio usare 'in.readList (myList, MyParcelableClass.class.getClassLoader()) ; ' – Wojtek

Problemi correlati