ViewModel returns ArrayList size as zero

Viewed 1000

I am using Android Paging library to load data from server in my RecyclerView. I am able to see the data properly but when i print the size of the list, it returns me zero.

Following is my code:

val itemViewModel =
                ViewModelProviders.of(this, MyViewModelFacotry(disciplineId, schoolId, yearId)).get(StudentInfoViewModel::class.java)
        activity?.let {
            itemViewModel.itemPagedList.observe(it, Observer<PagedList<StudentData>> { studentDataPagedList ->
                if(BuildConfig.DEBUG){
                    Log.i("MainActivity",studentDataPagedList?.size.toString())
                }
                studentInfoNameAdapter?.submitList(studentDataPagedList)
            })

            recyclerView.adapter = studentInfoNameAdapter
        }

The Log statement prints always zero. I want to show a TextView if list is empty but the list always returns size as zero.

3 Answers

When you receive the PagedList for the first time it could be empty if setEnablePlaceholders(false) was provided as configuration. If you want or can handle null values, you might enable placeholders. If there's just no data yet, you might want to look into different approaches.

You could wrap you adapter with a custom implementation which delegates the dataset changes properly and displays a single item when the original adapter has no items.

The PagedList documentation states in regard of the placeholder:

Placeholders

There are two ways that PagedList can represent its not-yet-loaded data - with or without null placeholders.

With placeholders, the PagedList is always the full size of the data set. get(N) returns the Nth item in the data set, or null if its not yet loaded.

Without null placeholders, the PagedList is the sublist of data that has already been loaded. The size of the PagedList is the number of currently loaded items, and get(N) returns the Nth loaded item. This is not necessarily the Nth item in the data set.

The Log statement prints always zero. I want to show a TextView if list is empty but the list always returns size as zero.

If list is empty, its size should be zero so it's correct behaviour.

If you're getting empty list all the time than the problem is somewhere else, propably in the place when you extract data and add it to the list.

I was able to show the empty state using RecyclerView.AdapterObserver as shown below:

Initialize and register the AdapterObserver

        val adapterDataObserver = object : RecyclerView.AdapterDataObserver() {

        override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
            super.onItemRangeInserted(positionStart, itemCount)
            showEmptyState(myAdapter.itemCount == 0)
        }

        override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
            super.onItemRangeRemoved(positionStart, itemCount)
            showEmptyState(myAdapter.itemCount == 0)
        }
    }
       myAdapter.registerAdapterDataObserver(adapterDataObserver)

Also, don't forget to unRegister the AdapterObserver in onDestroy()

Related