0

Updating value of recyclerview but unable to update corresponding data in model class

Model classes

 @Parcelize
class GetStockListData : ArrayList<GetStockListDataItem>(), Parcelable
@Parcelize
data class GetStockListDataItem(
    var Qty:@RawValue Double,
    var selectedQty: Double
): Parcelable

able to change recyclerview element using alertbox as follows

 private fun showAlertDialog(stockListData: GetStockListData, position: Int) {
    val layoutInflater = LayoutInflater.from(context)
    val customView =
        layoutInflater.inflate(R.layout.change_qty_dialog, null)
    val myBox: android.app.AlertDialog.Builder = android.app.AlertDialog.Builder(context)
    myBox.setView(customView)

    val dialog = myBox.create()
    dialog.show()

    val etQuantity = customView.findViewById<AppCompatEditText>(R.id.et_quantity)
    if (stockListData[position].Qty < args.getListDetailsByRNumberModelItem.ReqQty) {
        val df = DecimalFormat("#.##")
        df.roundingMode = RoundingMode.CEILING
        etQuantity.setText(df.format(stockListData[position].Qty).toString())
    } else
        etQuantity.setText(args.getListDetailsByRNumberModelItem.ReqQty.toString())


    etQuantity.setSelection(etQuantity.text.toString().length)
    etQuantity.requestFocus()
    requireContext().showKeyboard()

    customView.findViewById<Button>(R.id.btnDone).setOnClickListener {
        if(!etQuantity.text.isNullOrEmpty()) {
            val qtyStr = etQuantity.text.toString().trim()
            var qtyDouble = qtyStr.toDouble()
            stockListData[position].selectedQty = qtyDouble
            adapter.notifyDataSetChanged()
            dialog.dismiss()
        }
    }

}


 for (i in 0 until stockListData.size){
            sum += stockListData[i].selectedQty
        }

here if user edit Recyclerview list item multiple times, each value added to list. Finally if i try to retrieve sum of all recyclerview elements getting wrong value because in model class values are added when i try to replace element.

sri
  • 73
  • 1
  • 11

1 Answers1

0

Instead of passing whole list as parameter to showAlertDialog() method, you could just pass single item which has to be updated. And one more thing, you should not call adapter.notifyDataSetChanged() for single item updation, rather call adapter.notifyItemChanged(position). Look at below code, I am getting correct sum :

    private fun showRecipeMeasureDialog(recipeItem: RecipeItem?,position: Int){

    val dialogView = LayoutInflater.from(context).inflate(R.layout.add_recipe_measure_dialog, null)
    val dialog = AlertDialog.Builder(context, R.style.RoundedCornersDialog).setView(dialogView).show()

    dialog.setCancelable(false)
    val displayRectangle = Rect()
    val window = activity?.window
    window?.decorView?.getWindowVisibleDisplayFrame(displayRectangle)
    dialog.window?.setLayout(
        (displayRectangle.width() * 0.5f).toInt(),
        dialog.window!!.attributes.height
    )
    context?.resources?.let {
        dialogView.findViewById<TextView>(R.id.recipe_measure_title).text = java.lang.String.format(it.getString(R.string.addRecipeMeasure), unitsArray[currentMeasurementUnits - 1])
    }
    val doneBtn = dialogView.findViewById<ImageButton>(R.id.recipe_measure_done_btn)
    val closeBtn = dialogView.findViewById<ImageButton>(R.id.close_btn_add_recipe)
    val conversionEditText = dialogView.findViewById<ClearFocusEditText>(R.id.recipe_conversion_tie)
    doneBtn.isEnabled = false

    if (recipeItem != null ){
        conversionEditText.setText("${recipeItem.conversionRatio}")
    }

    closeBtn.setOnClickListener {
        context?.hideKeyboard(it)
        dialog.dismiss() }

    doneBtn.setOnClickListener {
        context?.hideKeyboard(it)
        val conversionRatio = conversionEditText.text.toString().toFloat()
        if (recipeItem != null){
            recipeItem.conversionRatio = conversionRatio
            recipeItemList[position] = recipeItem

            recipeAdapter.notifyItemChanged(position)
        }else{
            recipeItemList.add(0,RecipeItem(0,0,conversionRatio,0)) // Use your data class in place of RecipeItem
            recipeAdapter.notifyItemInserted(0)  // new Item is added to index zero, so adapter has to be updated at index zero
        }
        // calculating sum
        val sum = recipeItemList.map { it.conversionRatio }.sum()
        Log.d("tisha==>>","Conversion ratio sum = $sum")
        dialog.cancel()
    }
}
NRUSINGHA MOHARANA
  • 1,489
  • 8
  • 13