Home > Blockchain >  Recyclerview item shows empty spaces when stock count is 0
Recyclerview item shows empty spaces when stock count is 0

Time:09-17

when i get stock count is 0 from response the product is not shown but the product place shows empty spaces in grid recycler view kotlin. i pass the whole list into adapter and then filter it by stock count. if stock count is 0, the product won't show but in app it shows empty place for that

class ProductListAdapter(
    val productlists: List<Data>,
    val context: Context,
    val addCart: AddCartListener
) : RecyclerView.Adapter<ProductListAdapter.ProductListViewHolder>() {

    private val TAG = "ProductListAdapter"

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductListViewHolder {
        var itemView =
            LayoutInflater.from(parent.context).inflate(R.layout.product_list, parent, false)
        return ProductListViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: ProductListViewHolder, position: Int) {

        val product: Data = productlists[position]
        val variantslist: List<Variant> = product.variants
        holder.setIsRecyclable(false)

        holder.productname.text = product.name

        Glide.with(holder.itemView.context)
            .load(product.image)
            .into(holder.productimage)

        Log.e(TAG, "onBindViewHolder: "   product.variants[0].cart_count)

        if (variantslist.get(0).cart_count == 0) {
            holder.btn_addcart.visibility = View.VISIBLE
            holder.ll_cart.visibility = View.GONE
        } else {
            holder.btn_addcart.visibility = View.GONE
            holder.ll_cart.visibility = View.VISIBLE
            holder.tv_cart.text = variantslist.get(0).cart_count.toString()
        }

        holder.btn_addcart.setOnClickListener {
            variantslist.get(0).cart_count = variantslist.get(0).cart_count   1
            addCart.addCart(variantslist.get(0))
            notifyDataSetChanged()
        }

        holder.tv_addcart.setOnClickListener {
            variantslist.get(0).cart_count = variantslist.get(0).cart_count   1
            addCart.addCart(variantslist.get(0))
            notifyDataSetChanged()
        }

        holder.tv_removecart.setOnClickListener {
            variantslist.get(0).cart_count = variantslist.get(0).cart_count - 1
            addCart.addCart(variantslist.get(0))
            notifyDataSetChanged()
        }

        if (variantslist[0].stock.toInt() > 0) {
            holder.mrp.text = "₹"   "%.2f".format(variantslist[0].price.toDouble())
            holder.mrp.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
            holder.discountprice.text =
                "₹"   "%.2f".format(variantslist[0].discounted_price.toDouble())

            if (variantslist[0].measurement_unit_name == null) {
                holder.singlevariant.text = variantslist[0].measurement   " pcs"
                holder.multivariant.text = variantslist[0].measurement   " pcs"
            } else {
                holder.singlevariant.text =
                    variantslist[0].measurement   variantslist[0].measurement_unit_name
                holder.multivariant.text =
                    variantslist[0].measurement   variantslist[0].measurement_unit_name
            }
        } else {
            holder.singlevariant.visibility = View.GONE
            holder.multivariant.visibility = View.GONE
            holder.mrp.visibility = View.GONE
            holder.discountprice.visibility = View.GONE
            holder.productCardView.visibility = View.GONE
            holder.ProductConstraint.visibility = View.GONE
            holder.productimage.visibility = View.GONE
            holder.productname.visibility = View.GONE
            holder.btn_addcart.visibility = View.GONE
            holder.ll_cart.visibility = View.GONE
            holder.tv_addcart.visibility = View.GONE
            holder.tv_removecart.visibility = View.GONE
            holder.tv_cart.visibility = View.GONE
            holder.pricediff.visibility = View.GONE
            holder.pricedifference.visibility = View.GONE
        }

        var diffvalue =
            ((variantslist[0].price.toDouble() - variantslist[0].discounted_price.toDouble()))
        var percentage = (diffvalue / variantslist[0].price.toDouble()) * 100
        if (percentage > 3) {
            holder.pricediff.visibility = View.VISIBLE
            holder.pricedifference.visibility = View.VISIBLE
            holder.pricediff.text = percentage.toInt().toString()   "% \nOff"
        } else {
            holder.pricediff.visibility = View.GONE
            holder.pricedifference.visibility = View.GONE
        }

        if (variantslist.size > 1) {
            holder.multivariant.visibility = View.VISIBLE
            holder.singlevariant.visibility = View.GONE
            holder.multivariant.setOnClickListener {
                //need to show dropown
                showCustomDialog(product)
            }
        } else {
            //navigate product details page
            holder.singlevariant.visibility = View.VISIBLE
            holder.multivariant.visibility = View.GONE
        }

        holder.itemView.setOnClickListener {
            movefrag(product)
        }
    }

    open fun getUserId(): String? {
        val preferences = context.getSharedPreferences(
            "userInfo",
            Activity.MODE_PRIVATE
        ) //Frequent to get SharedPreferences need to add a step getActivity () method
        return preferences.getString("customer_id", "90336")
    }

    fun movefrag(product: Data) {
        addCart.navFragment(product)
    }

    fun showCustomDialog(product: Data) {

        val dialog = Dialog(context)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog.setCancelable(false)
        dialog.setContentView(R.layout.dialog_listview)
        val btndialog: Button = dialog.findViewById(R.id.btndialog) as Button
        btndialog.setOnClickListener { dialog.dismiss() }

        val listView: ListView = dialog.findViewById(R.id.listview) as ListView
        val adapter = VariantAdapter(context, product, addCart)
        listView.adapter = adapter
        listView.setOnItemClickListener(AdapterView.OnItemClickListener { parent, view, position, id ->
            Log.e(TAG, "showCustomDialog: "   adapter.getItem(position))
            dialog.dismiss()
        })
        dialog.setCanceledOnTouchOutside(true)
        dialog.show()
    }

    override fun getItemCount(): Int {
        return productlists.size
    }

    class ProductListViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var productimage: ImageView
        var productname: TextView
        var mrp: TextView
        var discountprice: TextView
        var btn_addcart: TextView
        var ll_cart: LinearLayout
        var tv_addcart: TextView
        var tv_removecart: TextView
        var tv_cart: TextView
        var multivariant: TextView
        var singlevariant: TextView
        var pricediff: TextView
        var pricedifference: ImageView
        var productCardView: CardView
        var ProductConstraint: ConstraintLayout

        init {
            productimage = itemView.productimage
            productname = itemView.productname
            mrp = itemView.mrp
            discountprice = itemView.Discountprice
            btn_addcart = itemView.btn_addcart
            ll_cart = itemView.ll_cart
            tv_addcart = itemView.tv_addcart
            tv_removecart = itemView.tv_removecart
            tv_cart = itemView.tv_cart
            multivariant = itemView.multivariant
            singlevariant = itemView.singlevariant
            pricediff = itemView.pricediff
            pricedifference = itemView.pricedifference
            productCardView = itemView.productcardview
            ProductConstraint = itemView.productconstraint
        }
    }
}

This is the image of Best Sellers , i should not get that empty between items

This is the Fragment Code

private fun getpopular() {

    LoadingUtils.showDialog(context, false)

    username = getUserId()

    val retrofitBuilder = Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl(BASE_L)
        .build()
        .create(ApiInterface::class.java)

    val retrofitData = retrofitBuilder.getpopularproductitems(90336, username!!)

    retrofitData.enqueue(object : Callback<productlist> {

        override fun onResponse(call: Call<productlist>, response: Response<productlist>) {
            Log.e("latest", "res-"   response.body())
            val responsebody: productlist = response.body()!!
            LoadingUtils.hideDialog()
            popularadapter = popularproductAdapter(frag, responsebody.data, addCart)
            popularadapter.notifyDataSetChanged()
            linearLayoutManager = LinearLayoutManager(context)
            popularproductsRView.adapter = popularadapter
        }

        override fun onFailure(call: Call<productlist>, t: Throwable) {
            Log.d("MainError", "onFailure: "   t.message)
            LoadingUtils.hideDialog()
        }
    })
}

CodePudding user response:

I am considering the productlist class as follows:

data class productlist(val uid: Int, val data: List<BasicTimer>,...)

And your Data class is as follows:

data class Data(val uid: Int, var stock_count: Int,...)

First thing first change the name for the variable data in productlist class as it is a keyword of Kotlin.

Now in the retrofitData.enqueue() filter the list which does have a stock_count > 0

Replace the following line :

popularadapter = popularproductAdapter(frag, responsebody.data, addCart)

with:

popularadapter = popularproductAdapter(frag, responsebody.data.filter{it.stock_count > 0}, addCart)

This will not send objects with stock_count = 0 to the RecyclerView to be rendered.

Better option is to use a ViewModel with the RecyclerView so that you can observer if the data has changed after the initial run.

  • Related