简体   繁体   中英

Pass ArrayList Class Object to another fragment using Bundle in Android

I need to pass ArrayList of class object to another fragment using Bundle.

I have tried this something like this from this post.

List< SubCateogory > subCatList = allLists.getResult().getCategory().get(position).getSubCategories();
Bundle bundle = new Bundle();
bundle.putParcelableArray(ApplicationVariables.SUB_CAT_LISTS, subCatList);

It displays following error. Wrong 2nd argument type. Found: 'java.util.List<com.healthcamp.healthapp.models.HomeCategory.SubCateogory>', required: 'android.os.Parcelable[]'

My Category, SubCategory classes implements Parceable along with required methods for 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 {...

Please suggest. Thank You.

You can use a putParcelableArrayList instead of putParcelableArray

Also , you need to define your instance as an ArrayList so change it to ArrayList< SubCateogory > subCatList

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM