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.