使用 Bundle in Android 将 ArrayList Class 对象传递给另一个片段
Pass ArrayList Class Object to another fragment using Bundle in Android
我需要使用 Bundle 将 class 对象的 ArrayList 传递给另一个片段。
我已经从这个 post.
尝试过类似的东西
List< SubCateogory > subCatList = allLists.getResult().getCategory().get(position).getSubCategories();
Bundle bundle = new Bundle();
bundle.putParcelableArray(ApplicationVariables.SUB_CAT_LISTS, subCatList);
显示如下错误。 Wrong 2nd argument type. Found: 'java.util.List<com.healthcamp.healthapp.models.HomeCategory.SubCateogory>', required: 'android.os.Parcelable[]'
我的类别、子类别 classes 实现 Parceable
以及 parceable 所需的方法。
Result.java
public class Results implements Parcelable {
@SerializedName("category")
@Expose
private List<Category> category = null;
public List<Category> getCategory() {
return category;
}
public void setCategory(List<Category> category) {
this.category = category;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
}
Category.java
public class Category implements Parcelable { ...
亚Category.java
public class SubCateogory implements Parcelable {...
求推荐。谢谢。
您可以使用 putParcelableArrayList
而不是 putParcelableArray
此外,您需要将实例定义为 ArrayList
,因此请将其更改为
ArrayList< SubCateogory > subCatList
我需要使用 Bundle 将 class 对象的 ArrayList 传递给另一个片段。
我已经从这个 post.
尝试过类似的东西List< SubCateogory > subCatList = allLists.getResult().getCategory().get(position).getSubCategories();
Bundle bundle = new Bundle();
bundle.putParcelableArray(ApplicationVariables.SUB_CAT_LISTS, subCatList);
显示如下错误。 Wrong 2nd argument type. Found: 'java.util.List<com.healthcamp.healthapp.models.HomeCategory.SubCateogory>', required: 'android.os.Parcelable[]'
我的类别、子类别 classes 实现 Parceable
以及 parceable 所需的方法。
Result.java
public class Results implements Parcelable {
@SerializedName("category")
@Expose
private List<Category> category = null;
public List<Category> getCategory() {
return category;
}
public void setCategory(List<Category> category) {
this.category = category;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
}
Category.java
public class Category implements Parcelable { ...
亚Category.java
public class SubCateogory implements Parcelable {...
求推荐。谢谢。
您可以使用 putParcelableArrayList
而不是 putParcelableArray
此外,您需要将实例定义为 ArrayList
,因此请将其更改为
ArrayList< SubCateogory > subCatList