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

155 Views Asked by At

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?

1

There are 1 best solutions below

0
Mir Milad Hosseiny On BEST ANSWER

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.