Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete/remove PagedListAdapter item

Currently I am using Android Architecture Components for App development everything is working along with paging library Now I want to remove recyclerview Item using PagedListAdapter to populate this we required to add a data source and from data source list is updating using LiveData no I want to remove a item from list notifyItemRemoved() is working from PagedList I am getting this exception:

java.lang.UnsupportedOperationException


java.util.AbstractList.remove(AbstractList.java:638)
like image 317
Faris Jameel Avatar asked Jan 09 '18 17:01

Faris Jameel


People also ask

How do I update PagedList?

You have to fetch the data from the server and then store it into local DB using room library and then whenever you have to update any item, update the item into local DB and in turn room will reflect those changes into the UI(using LiveData). But then you have to do the paging from using local DB.


1 Answers

Since you cannot directly modify the data that you have in the PagedList as I believe its immutable, the key to implementing the removal of items in this situation is maintaining a backing dataset somewhere in your code that represents all the data that you have received so far. An ArrayList worked for me.

When you want to remove an item, remove it from your backing dataset and call invalidate on your data source. This will trigger the loadInitial() call, in which you can pass your backing dataset to the callback.onResult() function. Your PagedListAdapter will use the DiffUtil.ItemCallback you supplied to smartly determine that there has been an update to its data and will update itself accordingly.

This tutorial helped me understand the correct flow to use in order to properly delete an item while using the paging library: https://medium.com/@FizzyInTheHall/implementing-swipe-to-delete-with-android-architecture-components-a95165b6c9bd

The repo associated with the tutorial can be found here: https://gitlab.com/adrianhall/notes-app

like image 168
Tyler Bennett Avatar answered Sep 20 '22 16:09

Tyler Bennett