I have a custom PositionalDataSource which loads data from Content Provider cursor.
class FinalizedInstanceDataSource() : PositionalDataSource<GeneralInstanceModel>() {
override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<GeneralInstanceModel>) {
callback.onResult(getInstances(params.loadSize, params.startPosition))
}
override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<GeneralInstanceModel>) {
callback.onResult(getInstances(params.requestedLoadSize, params.requestedStartPosition), 0)
}
private fun getInstances(limit: Int, offset: Int): MutableList<GeneralInstanceModel> {
val finalList = mutableListOf<GeneralInstanceModel>()
val cursor = InstancesDao().getFinalizedInstancesCursor(FormType.ALL, limit, offset)
....
}
The DataSource Factory is defined as;
class FinalizedInstaceDataSourceFactory(): DataSource.Factory<Int, GeneralInstanceModel>() {
val sourceLiveData = MutableLiveData<FinalizedInstanceDataSource>()
var latestSource: FinalizedInstanceDataSource? = null
override fun create(): DataSource<Int, GeneralInstanceModel> {
latestSource = FinalizedInstanceDataSource()
sourceLiveData.postValue(latestSource)
return latestSource!!
}
}
In my application user selects an item in the displayed list by checking a checkbox and performs some action. The item will then be removed in the database. To refresh the PagedList i call invalidate as;
listOfFinalizedInstances.value?.dataSource?.invalidate()
where
lateinit var listOfFinalizedInstances : LiveData<PagedList<GeneralInstanceModel>>
The result is that every item in the list above the selected item is not displayed. When calling invalidate(), it seems that params.requestedStartPosition value is equal to the position of the item that I selected in the list. for example, If i selected the third item in the list(there is a checkbox) and performed an action. After the action, i call invalidate(). The list is updated only above the third position. How can I solve this?