paulex
07/14/2020, 2:18 PMRecyclerView
that uses a PagedListAdapter
which fetches data from ROOM
and Network API. It uses a BoundaryCallback
to make request to the API of which the data returned is inserted into the Database(ROOM)
i have a list item that has an increment and decrement button...
I have attached an image of the list...
The current list is filterable, e.g i can filter the list of products by category
Problem
If i increase a product item quantity using the increment button, to e.g 12 and then i try to add more to filter the list by, the current list doesn't refresh which is fine because the DiffUtil.ItemCallback
confirms that the items are the same, but once i try to increment that same product's quantity after filtering by more category, it starts from zero again....
So i'm not really sure what the problem really is: below is the code that does the increment and decrement.
override fun onIncrementQuantity(position: Int, item: ProductEntity) {
item.quantity = item.quantity + 1
selectedProducts[item.item.id!!] = item
productAdapter?.notifyItemChanged(position, item)
}
override fun onDecrementQuantity(position: Int, item: ProductEntity) {
item.quantity = if (item.quantity == 0) 0 else item.quantity - 1
if (item.quantity == 0) {
selectedProducts.remove(item.item.id)
} else {
selectedProducts[item.item.id!!] = item
}
productAdapter?.notifyItemChanged(position, item)
}
Valentin Moscone
07/14/2020, 2:23 PMitem.quantity
has the last incremented value? (for e.g 12) I think that your ProductEntity object is 0 after all your filter actionspaulex
07/14/2020, 2:27 PMproductAdapter?.notifyItemChanged(position, item)
it displays 0 on the screen.... and resets itemValentin Moscone
07/14/2020, 2:29 PMonBindViewHolder()
after calling notifyItemChanged()
?paulex
07/14/2020, 2:34 PMnotifyItemChanged
is called, onBindViewHolder is triggered:
override fun onBindViewHolder(holder: KeugsViewHolder<T>, position: Int) {
getItem(position)?.let { holder.bind(it) }
}
The item retrieved from getItem
has a quantity of 0Valentin Moscone
07/14/2020, 3:13 PMitem.quantity
is being 0. New data is overriding that value or you are not updating the list that is being displayed in the recyclerview (your are always setting a new list with default values)paulex
07/14/2020, 3:16 PM* The payloads parameter is a merge list from {@link #notifyItemChanged(int, Object)} or
* {@link #notifyItemRangeChanged(int, int, Object)}. If the payloads list is not empty,
* the ViewHolder is currently bound to old data and Adapter may run an efficient partial
* update using the payload info. If the payload is empty, Adapter must run a full bind.
* Adapter should not assume that the payload passed in notify methods will be received by
* onBindViewHolder(). For example when the view is not attached to the screen, the
* payload in notifyItemChange() will be simply dropped.
*
* @param holder The ViewHolder which should be updated to represent the contents of the
* item at the given position in the data set.
* @param position The position of the item within the adapter's data set.
* @param payloads A non-null list of merged payloads. Can be empty list if requires full
* update.
*/
public void onBindViewHolder(@NonNull VH holder, int position,
@NonNull List<Object> payloads) {
onBindViewHolder(holder, position);
}