-2

I have created an app for my college project, it's an app to get the product quantity and store in data base.

The problem is when I decrement it goes below 0(-1,-2....) so i want to set the range, min 0 when i decrement and max 10 when i increment it.

Screenshot Screenshot

this is my code..

 override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    (holder as ViewHolder).bind(cartItems[position])

    holder.itemView.decrement_in_cart.setOnClickListener {
        val databaseHelper = DatabaseHelper(it.context)
        val item_name = holder.itemView.cart_item_name.text.toString()
        val count = holder.itemView.cart_item_count.text.toString().toInt()
        val count_new = databaseHelper.increase_decrease_in_cart("decrement", item_name, count)
        holder.itemView.cart_item_count.text = count_new.toString()
    }

    holder.itemView.increment_in_cart.setOnClickListener {
        val databaseHelper = DatabaseHelper(it.context)
        val item_name = holder.itemView.cart_item_name.text.toString()
        val count = holder.itemView.cart_item_count.text.toString().toInt()
        val count_new = databaseHelper.increase_decrease_in_cart("increment", item_name, count)
        holder.itemView.cart_item_count.text = count_new.toString()
    }

    holder.itemView.remove_from_cart.setOnClickListener {
        val databaseHelper = DatabaseHelper(it.context)
        val item_name = holder.itemView.cart_item_name.text.toString()
        databaseHelper.delete_from_cart(item_name)
        val cartFirebase = Cart_Firebase()
        cartFirebase.updateCart()
    }

}
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Vikkr
  • 1
  • 1
  • If you are using the Realtime Database you might also consider using transaction as explained [here](https://stackoverflow.com/questions/48307610/how-to-save-users-score-in-firebase-and-retrieve-it-in-real-time-in-android-stud), or if you're using Cloud Firestore, check [this](https://stackoverflow.com/questions/53126348/can-i-make-firestore-transaction-that-reads-from-firestore-but-write-on-the-rea/53129151) out. – Alex Mamo May 02 '21 at 06:13

2 Answers2

0
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    (holder as ViewHolder).bind(cartItems[position])

    holder.itemView.decrement_in_cart.setOnClickListener {
        val databaseHelper = DatabaseHelper(it.context)
        val item_name = holder.itemView.cart_item_name.text.toString()
        val count = itemCountCheck(holder.itemView.cart_item_count.text.toString().toInt())
        val count_new = databaseHelper.increase_decrease_in_cart("decrement", item_name, count)
        holder.itemView.cart_item_count.text = count_new.toString()
    }

    holder.itemView.increment_in_cart.setOnClickListener {
        val databaseHelper = DatabaseHelper(it.context)
        val item_name = holder.itemView.cart_item_name.text.toString()
        val count = itemCountCheck(holder.itemView.cart_item_count.text.toString().toInt())
        val count_new = databaseHelper.increase_decrease_in_cart("increment", item_name, count)
        holder.itemView.cart_item_count.text = count_new.toString()
    }

    holder.itemView.remove_from_cart.setOnClickListener {
        val databaseHelper = DatabaseHelper(it.context)
        val item_name = holder.itemView.cart_item_name.text.toString()
        databaseHelper.delete_from_cart(item_name)
        val cartFirebase = Cart_Firebase()
        cartFirebase.updateCart()
    }

}

private fun itemCountCheck(value: Int):Int {
   if(value < 0){
      return 0
   }else if(value > 10){
      return 10
   }else{
      return value
   }
}

You can achieve this by writing a minimal function.

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Arda Kazancı
  • 8,341
  • 4
  • 28
  • 50
  • I write an answer based on the values ​​entered. -1,-2,-3,-4 -> return 0 ||| 0 , 1, 2, 3 ,5...10 -> return 0 , 1, 2, 3 ,5...10 11 , 12 , 13.... -> return 10 Thank you @a_local_nobody – Arda Kazancı Apr 30 '21 at 11:59
  • i initially thought that this _might_ cause issues but i saw you assigned the result of the function to a variable and then use the variable so i suppose it works :) – a_local_nobody Apr 30 '21 at 12:03
0
val count = itemCountCheck(holder.itemView.cart_item_count.text.toString().toInt())
val count_new = databaseHelper.increase_decrease_in_cart("decrement", item_name, count)
holder.itemView.cart_item_count.text = count_new.toString()

You're

  • reading the current item_count
  • passing it to a function in databaseHelper and storing the result
  • setting that result as the new item_count in the display

so you should probably handle your bounding logic in databaseHelper, since that's the thing that's doing validation, computing a new count and telling you what it is.

(Really databaseHelper should hold the current count internally, and you just call "increment" or "decrement" with the item name, so it can update and give you a new value to display. You shouldn't need to read the display to tell it what's currently in the cart, y'know? This way is open to bugs and security issues)

cactustictacs
  • 17,935
  • 2
  • 14
  • 25