I am using ExpandableRecyclerView in my project to have a nested RecyclerView. It works great, except when you need to dynamically update the parent list.
My project have Product
class as parent object (and extended ParentListItem
). I am getting the list of Products from an API and thus need to clear the current list and add the List I have got.
In the ExpandableRecyclerAdapter I've tried this method -
public void onDataChanged(ArrayList<Product> parentObjects) {
this.getParentItemList().clear();
this.getParentItemList().addAll(parentObjects);
this.notifyItemRangeChanged(0, parentObjects.size() - 1);
}
However it's giving a strange generic error
Error:(60, 33) error: no suitable method found for addAll(ArrayList<Product>)
method List.addAll(Collection<? extends CAP#1>) is not applicable
(actual argument ArrayList<Product> cannot be converted to Collection<? extends CAP#1> by method invocation conversion)
method List.addAll(int,Collection<? extends CAP#1>) is not applicable
(actual and formal argument lists differ in length)
method Collection.addAll(Collection<? extends CAP#1>) is not applicable
(actual argument ArrayList<Product> cannot be converted to Collection<? extends CAP#1> by method invocation conversion)
where CAP#1 is a fresh type-variable:
CAP#1 extends ParentListItem from capture of ? extends ParentListItem
The parentItemList is of type List<? extends ParentListItem>
, so according to this question, it's not possible to add anything to the existing list and since there's no setter present, I can't replace the complete list too.
I have tried creating a new ExpandableRecyclerAdapter
object and setting it as a new adapter for RecyclerView. Although it worked, but I don't think that's a good way to replace whole adapter whenever there's a new data coming.
So is there any other way to update the parent object list, either in one go or one by one?