Afternoon... I have a `RecyclerView` that uses a ...
# android
p
Afternoon... I have a
RecyclerView
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.
Copy code
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)
}
stackoverflow 2
v
Are you sure that
item.quantity
has the last incremented value? (for e.g 12) I think that your ProductEntity object is 0 after all your filter actions
p
I have used the debugger to check the quantity value... it is 12 and when it gets to the line where
productAdapter?.notifyItemChanged(position, item)
it displays 0 on the screen.... and resets item
v
did you check
onBindViewHolder()
after calling
notifyItemChanged()
?
p
Okay let me check the onBindViewHolder...
Okay Valentine... i think you might have given me an hint..
💯 1
So this is what happens... • list is initially loaded with data • updates are done on the current data • Once new data is fetched, onBindViewHolder is not called if the itemsAretheSame or the ContentsAreThemSame thus no need to updateView However it seems the currentList has been updated... so when
notifyItemChanged
is called, onBindViewHolder is triggered:
Copy code
override fun onBindViewHolder(holder: KeugsViewHolder<T>, position: Int) {
    getItem(position)?.let { holder.bind(it) }
}
The item retrieved from
getItem
has a quantity of 0
v
Ok! I see...you have to check the way you are updating the objects. And see way after fetching new data your
item.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)
p
Yeah, I think overriding the below code to do some partial update will fix my issue
Copy code
* 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);
}
FIXED IT FOR ME... Thanks You were really helpful... got a different perspective
👏 1