1

I am trying to achieve paging in my app using android paging library.But I am stuck in one place. The dataSource is not getting created from the factory. Snippet below.

private fun getLivePagedListBuilder(queryString: String): LivePagedListBuilder<Int, News> {

        val dataSourceFactory = object : DataSource.Factory<Int, News>() {
            override fun create(): DataSource<Int, News> {
                return NewsDataSource(queryString)
            }
        }
        return LivePagedListBuilder(dataSourceFactory, config)
    }

1. The create method is not getting called. So the return inside tht method not firing.

My DataSource

class NewsDataSource(val searchQuery: String) : PageKeyedDataSource<Int, News>() {
        override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, News>) {
            api.searchNews(searchQuery, Constants.perPageLimit, 1)
                .enqueue(object : Callback<NewsResponse> {
                    override fun onFailure(call: Call<NewsResponse>, t: Throwable) {
                        Log.d("TAG1", "Failure")
                    }

                    override fun onResponse(call: Call<NewsResponse>, response: Response<NewsResponse>) {
                        callback.onResult(response.body()?.news, response.body()?.page - 1, response.body()?.page + 1)
                    }
                })
        }
    }

The API returns the page number and I am planning to load the next page when the scroll ends

  1. What determines the datatype of the Key in DataSource.Factory<Int, News>()

I am really stuck on this :(

Droidme
  • 1,223
  • 6
  • 25
  • 45

0 Answers0