androidandroid-roomandroid-pagedlistview

Android Room how to search by text when using DataSource.Factory?


In my project using PagedListAdapter for pagination and the app also able to search by keyword.

DAO.kt

@Query("SELECT * FROM Item WHERE name LIKE :keyword")
fun getItems(keyword: String): DataSource.Factory<Int, ItemEntity>

Create

lateinit var itemLivePagedList: LiveData<PagedList<Item>>

.
.
.

val dataSourceFactory = itemUseCase.getItems(keyword)
val boundaryCallback = MyBoundaryCallback(itemUseCase, compositeDisposable, PAGE_SIZE)
itemLivePagedList = LivePagedListBuilder(dataSourceFactory, pagedListConfig)
            .setBoundaryCallback(boundaryCallback)
            .build()

How can I update the list when keyword changed?


Solution

  • When keyword changes, you have to call itemUseCase.getItems(keyword) again.

    Look at this project. it has been written in Java but it shows how to implement a search scenario with PagingLibrary.

    For more details see this answer.