1

I'm trying to use the new paging library for Android coding in Kotlin but am really stuck at the moment. My backend uses post method for connecting with the api calls and I'm trying to adapt the tutorials I've found using get but not being successful so far. Any help much appreciated indeed.

That's how my adapter is being called from my Fragment class but it's being always null.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    setVerticalRecyclerView(rv_resources)

    val itemViewModel = ViewModelProviders.of(this).get(ItemViewModel::class.java)
    val adapter = ResourcesAdapter(activity as MainActivity)
    itemViewModel.itemPagedList.observe(this, object : Observer<PagedList<Resource>> {
        override fun onChanged(items: PagedList<Resource>?) {
            adapter.submitList(items)
        }
    })

    rv_resources.adapter = adapter

}

I feel the problem is probably coming from here:

class ItemDataSource : PageKeyedDataSource<Int, Resource>() {

override fun loadInitial(params: PageKeyedDataSource.LoadInitialParams<Int>, callback: PageKeyedDataSource.LoadInitialCallback<Int, Resource>) {

      getResources()
}

override fun loadBefore(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, Resource>) {

    getResources()              
}

override fun loadAfter(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, Resource>) {

    getResources()        
}

private fun getResources() {

    val jo = JsonObject()
    jo.addProperty("page", 0)
    jo.addProperty("page_size", 10)

    GetAllResourceListAPI.postData(jo, object : GetAllResourceListAPI.ThisCallback {
        override fun onSuccess(getResourceList: GetResourceList) {
            Toast.makeText(App.getContext(), "onSuccess ${getResourceList.count}", Toast.LENGTH_SHORT).show()
        }

        override fun onFailure(failureMessage: String) {
            Toast.makeText(App.getContext(), "onFailure", Toast.LENGTH_SHORT).show()
        }

        override fun onError(errorMessage: String) {
            Toast.makeText(App.getContext(), "onError", Toast.LENGTH_SHORT).show()
        }
    })
}

Initially I'm trying to show any page within my adapter that's why pasted the same codes for loadInitial, loadBefore and loadAfter trying to tackle a problem at time if possible as currently my adapter shows empty even though I get a success from my api call. I may be missing something pretty obvious here but just can't see it as it's my first time using pagination and not very familiar with observers either.

I have a gist with a bit more of my code created here

Thanks very much for your help.

Francislainy Campos
  • 1,743
  • 11
  • 36
  • have you implemented pagedList configuration ? – Moinkhan Dec 18 '18 at 05:59
  • Hi @Moinkhan thank you for your message.Sorry what do you mean by paged list configuration? Having my adapter extend PagedListAdapter? Yes, thats done. `class ResourcesAdapter(private val context: Context) : PagedListAdapter(DIFF_CALLBACK) { ` – Francislainy Campos Dec 18 '18 at 10:57
  • You are missing pagedListBuilder – Moinkhan Dec 18 '18 at 11:02
  • Hi @Moinkhan I created a gist with the files I have here: gist.github.com/francislainy/0eca633f0027f5bfca7c260a769a609a – The PagedList builder is under the itemViewModel class – Francislainy Campos Dec 21 '18 at 13:11
  • ```public ItemViewModel() { ItemDataSourceFactory itemDataSourceFactory = new ItemDataSourceFactory(); liveDataSource = itemDataSourceFactory.getItemLiveDataSource(); PagedList.Config config = (new PagedList.Config.Builder()) .setEnablePlaceholders(false) .setPageSize(ItemDataSource.Companion.getPAGE_SIZE()) .build(); itemPagedList = (new LivePagedListBuilder(itemDataSourceFactory, config)).build(); }``` – Francislainy Campos Dec 21 '18 at 13:13
  • @FrancislainyCampos Did you get the answer? – Pratik Butani Feb 04 '19 at 04:51
  • Hi @pratik, not really so had to go back to my old implementation and make it work that way instead. I guess I'm not able to update my app to use the paging library at the moment as my whole backend uses post method and I don't know how to make it work for this scenario. – Francislainy Campos Feb 04 '19 at 05:00
  • Okay, Cool! I have posted [question](https://stackoverflow.com/questions/39825125/android-recyclerview-cursorloader-contentprovider-load-more) before, may it helps you in some way. – Pratik Butani Feb 04 '19 at 05:05
  • Thanks @PratikButani. Looked at your answer but didn't see many similarities with my question to be honest as couldn't see the post method being used there. But thanks for your good will to try and help me with this issue. Appreciate that. – Francislainy Campos Feb 08 '19 at 18:52

0 Answers0