title: Android paging 3: It is possible to get the itemcount from PagingData<T>?
body: <p>how am I able to get the current amount of items, that my <code>PagingData&lt;Product&gt;</code> holds? The only solution I found was to call <code>shopListAdapter.itemCount</code> but this always returns 0.</p>
  <p>What I am trying to do is: Get the current amount of items that are currently displayed by my PagingAdapter, submit this value to my shopViewModel, and then use this value inside another fragment (DialogFragment). Or: Get the amount of items from my <code>PagingData&lt;Product&gt;</code> inside my viewmodel</p>
  <p>Here is my current approach</p>
  <h2>MainFragment</h2>
  <pre><code>@AndroidEntryPoint
  class ShopFragment : Fragment(R.layout.fragment_shop), ShopAdapter.OnItemClickListener {
      private val shopViewModel: ShopViewModel by navGraphViewModels(R.id.nav_shop) { defaultViewModelProviderFactory }
      private var _shopBinding: FragmentShopBinding? = null
      private val shopBinding: FragmentShopBinding get() = _shopBinding!!
      @Inject lateinit var shopListAdapter: ShopAdapter
  
      override fun onCreateView(
          inflater: LayoutInflater,
          container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View {
          _shopBinding = FragmentShopBinding.inflate(inflater, container, false)
          return shopBinding.root
      }
  
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
          collectShopList()
      }
  
      private fun collectShopList(){
          shopViewModel.shopPagingData.observe(viewLifecycleOwner)  {
              shopListAdapter.submitData(viewLifecycleOwner.lifecycle, it)
              shopViewModel.setItemAmount(shopListAdapter.itemCount)
          }
      }
  }
  </code></pre>
  <h2>DialogFragment</h2>
  <pre><code>@AndroidEntryPoint
  class ShopFilterFragment : DialogFragment() {
      private val shopViewModel: ShopViewModel by navGraphViewModels(R.id.nav_shop) { defaultViewModelProviderFactory }
  
      override fun onCreateView(
          inflater: LayoutInflater,
          container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
          return inflater.inflate(R.layout.fragment_shop_filter, container, false)
      }
  
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
          shopViewModel.itemAmount.observe(viewLifecycleOwner) {
              toast(it.toString()) &lt;-- Always displays 0
          }
      }
  
  }
  </code></pre>
  <h2>ViewModel</h2>
  <pre><code>class ShopViewModel @ViewModelInject constructor(
      private val shopRepository: ShopRepository
  ) : ViewModel() {
      private val _itemAmount = MutableLiveData&lt;Int&gt;()
      val itemAmount: LiveData&lt;Int&gt; get() = _itemAmount
  
      private val query = MutableLiveData(QueryHolder(null, null, null))
  
      val shopPagingData = query.switchMap { query -&gt; shopRepository.search(query).cachedIn(viewModelScope) }
  
      fun search(newQuery: QueryHolder) {
          query.value = newQuery
      }
  
      fun setItemAmount(amount: Int) {
          _itemAmount.value = amount
      }
  }
  </code></pre>