1

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?

Santosh
  • 153
  • 1
  • 13
  • what is "selected item"? – pskink Mar 29 '19 at 09:38
  • @pskink A list row has a checkbox in it. Selected item is a row item whose checkbox is selected. Is it more clear? – Santosh Mar 29 '19 at 09:45
  • are you using `PagedListAdapter`? if so whats your code? what do you pass to `submitList()` method – pskink Mar 29 '19 at 09:53
  • I am using PagedListAdapter. I observe the above listOfFinalizedInstances livedata. The value of this live data is updated to submitList() – Santosh Mar 29 '19 at 09:59
  • what data is passed to `submitList`? did you `Log.d` that list? is it correct or not? if you dont know how to use `Log.d` to show it, try to set a breakpoint where you call `submitList` and verify what data you got from your `DataSource` – pskink Mar 29 '19 at 10:00
  • The is some invalid data. If I selected an item in position 3. The data contains item above that position. i.e item in position 4, 5, 6... . Items in position 1 and 2 are not displayed. – Santosh Mar 29 '19 at 10:04

0 Answers0