Как написать List <> в пакет
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 мой код Я использую модель, которая реализуется из разумного... Может ли кто-нибудь сказать, какой крик я пишу в этой строке category.setCategoryVideos(parcel.readTypedList())
Я не нашел полезного сообщения.
EDIT: category.setCategoryVideos(parcel.readTypedList(mCategoryVideos,Video.CREATOR));
здесь mCategoryVideos Я не могу разрешить ошибку.
Ответы
Ответ 1
Существуют методы списка для класса Parcelable, вы можете посмотреть на них здесь:
readList (List outVal, загрузчик ClassLoader)
writeList (List val)
В вашем случае это будет выглядеть так:
List<Object> myList = new ArrayList<>();
parcel.readList(myList,List.class.getClassLoader());
category.setCategoryVideos(myList);
Ответ 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
});
}
Затем In ur Activity.
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
}
}
Ответ 3
От хорошего ответа:
Простые шаги:
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);
}