【问题标题】:Pass ArrayList Class Object to another fragment using Bundle in Android在 Android 中使用 Bundle 将 ArrayList 类对象传递给另一个片段
【发布时间】:2018-02-11 16:29:11
【问题描述】:

我需要使用 Bundle 将类对象的 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&lt;com.healthcamp.healthapp.models.HomeCategory.SubCateogory&gt;', required: 'android.os.Parcelable[]'

My Category、SubCategory 类实现 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 { ...

SubCategory.java

public class SubCateogory implements Parcelable {...

请提出建议。谢谢。

【问题讨论】:

  • 请勿发布代码截图,请将代码复制/粘贴到您的帖子中。
  • 你明白required: 'android.os.Parcelable[]的意思了吗?它需要Parcelable 对象的array,而不是列表。
  • @JimGarrison 好的。我删除了屏幕截图。是的,正如@hacksy 所建议的那样,使用了putParcelableArrayList,它可以工作。

标签: java android android-fragments arraylist bundle


【解决方案1】:

您可以使用putParcelableArrayList 代替putParcelableArray

另外,您需要将您的实例定义为ArrayList,因此将其更改为 ArrayList&lt; SubCateogory &gt; subCatList

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多