How to clear/remove all items in page list adapter
Asked Answered
S

5

12

I'm using the android paging library to show search result items, is there any way I can clear/remove all the loaded result items, Calling Invalidate on live Paged List refreshing the list not clear/remove items

In Activity: 
 fun clearSearchResult() {
        if (productSearchResultItemAdapter.itemCount > 0) {
            viewModel.invalidateResultList()
        }
    }


In ViewModel
 private fun searchProductByTerm(searchTerm: String): Listing<Item> {
        sourceFactory = ProductSearchItemDataSourceFactory(productSearchUC, compositeDisposable, searchTerm, resourceResolver)

        val livePagedList = LivePagedListBuilder(sourceFactory, pagedListConfig)
                //The executor used to fetch additional pages from the DataSource
                .setFetchExecutor(getNetworkExecutor())
                .build()

        return Listing(
                pagedList = livePagedList,
                networkState = switchMap(sourceFactory.sourceLiveData) {
                    it.networkState
                },
                retry = {
                    sourceFactory.sourceLiveData.value?.retryAllFailed()
                }
        )

    }


    fun invalidateResultList() {
        sourceFactory?.sourceLiveData?.value?.invalidate()
    }

private val productSearchName = MutableLiveData<String>()
    private val repoResult = map(productSearchName) {
        searchProductByTerm(it)
    }
Slaughterhouse answered 24/2, 2019 at 21:55 Comment(1)
did you find any working solution for this?Erin
P
33

If you're working with PagingDataAdapter, searchAdapter.submitData(lifecycle, PagingData.empty()) works

Polemist answered 21/8, 2020 at 5:37 Comment(0)
S
9

submitting null clear the currently loaded page list

  productSearchResultItemAdapter.submitList(null)
Slaughterhouse answered 26/2, 2019 at 12:41 Comment(3)
This is the solution I was looking for :)Founder
I keep getting this error: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position. What can l do about it?Barbados
it doesn't always work, it still can keep old items while adding newsInsomnia
V
4

In Java:

I cleared all items on in PagedListAdapter by calling invalidate() on DataSource instance like that

public void clear(){
   movieDataSource.invalidate();
}

Add this method in your ViewModel then call it in your activity

movieViewModel.clear();
movieAdapter.notifyDataSetChanged();

Then Load any data you want

You can see how I made it in my project.

Here is the Link: https://github.com/Marwa-Eltayeb/MovieTrailer

Vida answered 1/12, 2019 at 18:53 Comment(0)
H
1

In Fragment

lifecycleScope.launch {
                viewModel.currentResult = null
                viewModel.getSearchAudio(binding.etxtSearch.text.toString().trim(), 0).collectLatest { it ->
                    Log.v(mTAG, "Status: New record")
                    adapterAudioList.submitData(it)
                }
            }
               

In ViewModel

var currentResult: Flow<PagingData<AudioModel>>? = null
fun getSearchAudio(trackName: String, lastPageCount: Int): Flow<PagingData<AudioModel>> {
    val lastResult = currentResult
    if (lastResult != null) {
        return lastResult
    }
    val newResult: Flow<PagingData<AudioModel>> = videoRepository.getAudioSearchPaging(trackName, lastPageCount).cachedIn(viewModelScope)
    currentResult = newResult
    return newResult
}

In videoRepository

fun getAudioSearchPaging(trackName: String, lastPageCount: Int): Flow<PagingData<AudioModel>> {
    return Pager(
        config = PagingConfig(pageSize = KeyConstants.AUDIO_PAGE_SIZE, enablePlaceholders = false),
        pagingSourceFactory = { AudioSearchPagingSource(context, trackName, lastPageCount) },
    ).flow
}
Hagen answered 2/5, 2021 at 0:26 Comment(1)
Sunny it serves only for one search. Next time the seach value doesn't change.Rizal
C
-3

Before invalidate, clear your list data item. Like we did in simple way:

list.clear();
adapter.notifyDataSetChanged();
Cantilena answered 25/2, 2019 at 5:0 Comment(1)
can you provide an example how to clear the data in pageListSlaughterhouse

© 2022 - 2024 — McMap. All rights reserved.