【发布时间】:2021-03-04 06:32:38
【问题描述】:
更新recyclerview的值但无法更新模型类中的对应数据
模型类
@Parcelize
class GetStockListData : ArrayList<GetStockListDataItem>(), Parcelable
@Parcelize
data class GetStockListDataItem(
var Qty:@RawValue Double,
var selectedQty: Double
): Parcelable
可以如下使用 alertbox 更改 recyclerview 元素
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
}
如果用户多次编辑 Recyclerview 列表项,则每个值都添加到列表中。最后,如果我尝试检索得到错误值的所有 recyclerview 元素的总和,因为在我尝试替换元素时在模型类中添加了值。
【问题讨论】:
标签: android kotlin android-recyclerview parcelable