Kishor Bikram Oli
Kishor Bikram Oli

Reputation: 1838

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.

Upvotes: 0

Views: 664

Answers (1)

hacksy
hacksy

Reputation: 840

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

Upvotes: 3

Related Questions