0

I'm getting stuck on fetching data to the recycler View I don't Know Where is the problem exactly if it is from the Firebase Query or from setting up the adapter for the recycler view, the recyclerview creates the exact number of the items that exist in the Firebase but, No information to be shown this is what I tried

FireBase

fun GetProductsList (fragment : Fragment) {
        var productList: ArrayList<Products> = ArrayList()
        mFireStore.collection("products")
            .get()
            .addOnSuccessListener { docSnapshot ->

                for (document in docSnapshot.documents) {
                    val productToAddToList : Products = document.toObject(Products::class.java)!!
                    productList.add(productToAddToList!!)
                }
                when (fragment) {
                    is AdminProductFragment -> {
                        fragment.SuccesProductListFromFireStore(productList)
                    }
                }

Adapter

toverride fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            val Model = productsList[position]
        if (holder is MyViewHolder ) {
            GlideLoader(context).loadProductPicture(Model.Image, holder.itemView.iv_item_image)
            holder.itemView.tv_item_name.text = Model.Name.toString()
            holder.itemView.tv_item_price.text = "${Model.Price.toString()} DZA"
        }
    }

And This

if (productList.size > 0 ) {
            recyclerViewProduct.visibility = View.VISIBLE
            text_no_productFounds.visibility = View.GONE
            recyclerViewProduct.layoutManager = LinearLayoutManager(activity)
            recyclerViewProduct.setHasFixedSize(true)
            val adapterProduct = MyProductsListAdapter(requireActivity(), productList)
            recyclerViewProduct.adapter = adapterProduct
        }else{
            recyclerViewProduct.visibility = View.GONE
            text_no_productFounds.visibility = View.VISIBLE
        }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Where exactly in your code are you checking `productList.size > 0`? – Alex Mamo Mar 30 '22 at 10:59
  • In the OnRsume Method of The Products Fragment – Merabti Abdelfateh Mar 30 '22 at 11:01
  • There is no way you can do that. Checking `productList.size > 0` inside onResume, will always be false. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a callback. You might also be interested in reading this article, [How to read data from Cloud Firestore using get()?](https://medium.com/firebase-tips-tricks/how-to-read-data-from-cloud-firestore-using-get-bf03b6ee4953). – Alex Mamo Mar 30 '22 at 11:08

0 Answers0