2013-03-21 2 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]; 
     } 
    }; 

} 

임 내가 parcelable에서 구현 모델을 사용하고 내 코드 ... 사람이 내가 어떤 도움이 게시물을 찾을 수 없습니다 category.setCategoryVideos(parcel.readTypedList()) 이 줄을 쓰기 무슨 소리 그들을 말할 수 없습니다.소포에 목록 <>을 작성하는 방법을

편집 : category.setCategoryVideos(parcel.readTypedList(mCategoryVideos,Video.CREATOR)); 여기 mCategoryVideos 오류를 해결할 수 없습니다.

답변

23

Parcelable 클래스의 목록 방법이 있습니다, 당신은 여기에서 좀 걸릴 수 있습니다 :

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

parcel.readList(myList,List.class.getClassLoader()); 
category.setCategoryVideos(myList); 
: 귀하의 경우

readList (List outVal, ClassLoader loader)

writeList (List val)

이의 모습

+1

이 답변으로 올바른 표시되어야한다, 한 가지의 상점은 목록 Parcelable뿐만 아니라 개체를 포함해야한다는 것입니다. –

+3

아주 작은 교정 :'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 
    }); 
} 

다음 활동 중.

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:

쉬운 단계 :

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

리스트를 읽을 때 필자는'in.readList (myList, MyParcelableClass.class.getClassLoader())를 사용하는 편이 낫다. ;' – Wojtek

관련 문제