Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

How to delete/remove PagedListAdapter item #281

Closed
@faris-jameel

Description

@faris-jameel

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)

Activity

sopherwang

sopherwang commented on Jan 15, 2018

@sopherwang

I had same issue.

faris-jameel

faris-jameel commented on Jan 15, 2018

@faris-jameel
Author

@JiajunWong did you find any solution?

tuinui

tuinui commented on Feb 6, 2018

@tuinui

i got this problem to when i try to Collections.sort

guness

guness commented on Feb 19, 2018

@guness

I have created some custom adapter to handle this case(only a part of it actually). Luckily I was supposed to add on top of current list, so following solution only supports adding to top. However you can try to customize it.

https://gist.github.com/guness/df12d8cc4f595af1395f4a1f5bca5f00

In addition, I think some kind of merging support for the data sources would be a nice to have feature for pagination library.

jakekim0

jakekim0 commented on May 18, 2018

@jakekim0

i had same issue

shashankkapsime

shashankkapsime commented on Jun 19, 2018

@shashankkapsime

@yigit Can anyone answer this issue please.

Tenkei

Tenkei commented on Jun 24, 2018

@Tenkei

Already had this discussion here:

If you have a collection shared w/ a RecyclerView, that collection CANNOT be modified w/o telling RecyclerView. So all modification & notifications needs to happen on the main thread and in the same call stack.

We could technically create a DataSource API that allows passing such partial changes but then we would need to log all of them and re-deliver to the RV on the main thread. The issue w/ that approach is that, if you have a RecyclerView that is stopped (a.k.a back stack), it will not (should not) receive any updates so PagedList would be keeping this potentially long list of items and re-apply on main thread for each observer (again some observers may have received some events).

This greatly complicates the problem, which is why we go w/ a single list.

Also, none of those optimizations would work w/ sqlite (so yea, that had a part). You can easily create a DataSource that can take a previous list and keep modifications on it, but still return it as a list. In other words, implement that tracking logic in your DataSource which would not need to deal w/ RecyclerView's thread restrictions.

TL;DR: To update a pagedList you have to create a new list, this is how room update data and you have to do the same if you want to update your list.

CarGuo

CarGuo commented on Jun 28, 2018

@CarGuo

I try this after delete local data

liveModel?.pageList?.dataSource?.invalidate()
adam-hurwitz

adam-hurwitz commented on Sep 1, 2018

@adam-hurwitz

After re-reading the documentation under Consider How Content Updates Work it appears the only way to have realtime updates is via implementing Room with the PagedList: If you're loading data directly from a Room database updates get pushed to your app's UI automatically.

I'm planning to connect my Firestore queries to Room in order to be able to remove items from my PagedList without having to invalidate the DataSource and reload the entire RecyclerView.

abhinav272

abhinav272 commented on Sep 15, 2018

@abhinav272

@AdamSHurwitz
If you connect your server data with Room to create PagedList from it, even then when you delete an item from your local DB your DataSource will be invalidated automatically and you will get another version of PagedList from Room in your onChanged()
Am I right?

adam-hurwitz

adam-hurwitz commented on Sep 15, 2018

@adam-hurwitz

@abhinav272 This is not the case. When an individual item in Room is added or modified the PagedList updates the corresponding entry rather than refreshing all of the data in the component. Therefore I'm able to update Room with Firestore data when it changes and allow Room + PagedList to handle the updates on individual cells. As long as Room is updated of the change the PagedList will animate and update/add/remove the item modified.

Conversely, if Firestore data was connected directly to the PagedList the entire data set would need to be invalidated / refreshed since the PagedList cannot automatically recognize individual changes from Firestore.

abhinav272

abhinav272 commented on Sep 15, 2018

@abhinav272

@AdamSHurwitz
Do you get a callback in onChanged() when you update the data in Room??
If so, that means Room has invalidated the DataSource and has provided new one with PagedList containing new data.

When I create a DataSource which is fetching data from either Room or Network, it gives me a callback in onChanged() only once when the PagedList gets created (upon creation of DataSource) and when the pages are loaded it automatically gets displayed on UI..

Make sense?

adam-hurwitz

adam-hurwitz commented on Sep 20, 2018

@adam-hurwitz

@abhinav272 - May you specify which onChangedd() method you are referring to?

I don't make Room updates directly. I update Firestore's database. With Firestore when a piece of information changes the Firestore listener will send an update. I listen for that update and inform Room of the change using a LiveData object. This makes it easy to keep both my backend Firestore and front-end Room data synced with minimal work.

Dilip23

Dilip23 commented on Oct 2, 2018

@Dilip23

@abhinav272

I had the same kind of issue!!!
I am building an app that receives data from network API which gets chached in Room DB. Now if i have to delete an item ,I send a request to delete an item in Remote DB(Network) and if the response is 204 , I delete an item in the Room , So then the Room DB creates a new PagedList and notifies the UI.... Right??

Or else Do we have to implement in any other way???

abhinav272

abhinav272 commented on Oct 3, 2018

@abhinav272

@Dilip23 yeah, this looks fine.

34 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ianhanniballake@alexandru-calinoiu@dlam@frog1014@adam-hurwitz

        Issue actions

          How to delete/remove PagedListAdapter item · Issue #281 · android/architecture-components-samples