kotlin 从ListView适配器中的函数内部调用时,文本视图数据未更新

68bkxrlz  于 2022-12-13  发布在  Kotlin
关注(0)|答案(1)|浏览(135)

从ListView适配器中的函数内部调用时,未更新文本视图数据。请帮助设置binding.tvQ.text

class CustomPizzaListAdapter (private val context: Context, private val pizzaItemList: ArrayList<PizzaItemList>) : BaseAdapter() {
    private lateinit var binding: CustomPizzaItemListLayoutBinding

    override fun getCount(): Int {
        return pizzaItemList.size
    }

    override fun getItem(position: Int): Any {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    @SuppressLint("ViewHolder")
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        binding = CustomPizzaItemListLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false)

        val current = pizzaItemList[position]
        Picasso.get().load(current.img).into(binding.imgPizza)
        binding.tvPizzaTitle.setText(current.Name)
        binding.tvPizzaDesc.setText(current.Desc)
        setPrice(binding.rgSize.checkedRadioButtonId, position)

        val mPosition = position
        binding.rgSize.setOnCheckedChangeListener { group, checkedId ->
            setPrice(checkedId, mPosition)
        }

        binding.btnPlusQ.setOnClickListener {
            binding.tvQ.text = (binding.tvQ.text.toString().toInt() + 1).toString()
            Toast.makeText(context, binding.tvQ.text, Toast.LENGTH_LONG).show()
            setPrice(binding.rgSize.checkedRadioButtonId, mPosition)
        }

        binding.btnMinusQ.setOnClickListener {
            if(binding.tvQ.text.toString().toInt() > 0){
                binding.tvQ.text = (binding.tvQ.text.toString().toInt() - 1).toString()
                Toast.makeText(context, binding.tvQ.text, Toast.LENGTH_LONG).show()
                setPrice(binding.rgSize.checkedRadioButtonId, mPosition)
            }
        }

        return binding.root
    }

    private fun setPrice(checkedId: Int, position: Int) {
        val current = pizzaItemList[position]
        val price: Float
        when (checkedId) {
            binding.rgSmall.id -> {
                price = binding.tvQ.text.toString().toInt() * current.smPrice!!
                binding.tvPrice.text = "Price Small $$price"
                Toast.makeText(context, "Price Small $price", Toast.LENGTH_SHORT).show()
            }
            binding.rgMedium.id -> {
                price = binding.tvQ.text.toString().toInt() * current.mPrice!!
                binding.tvPrice.text = "Price Medium $$price"
                Toast.makeText(context, "Price Medium $price", Toast.LENGTH_SHORT).show()
            }
            binding.rgLarge.id -> {
                price = binding.tvQ.text.toString().toInt() * current.lgPrice!!
                binding.tvPrice.text = "Price Large $$price"
                Toast.makeText(context, "Price Large $price", Toast.LENGTH_SHORT).show()
            }
            else -> {
                Toast.makeText(context,"Wrong",Toast.LENGTH_LONG).show()
            }
        }
    }
}

当我尝试从textview获取文本时,它返回吐司中textview的正确更新数据,但toast没有更新。尝试搜索如何更新视图,但没有任何效果。

2admgd59

2admgd591#

您不应具有此绑定属性:

private lateinit var binding: CustomPizzaItemListLayoutBinding

每次调用getView()时都要设置它,所以它总是最近滚动到屏幕上或即将滚动到屏幕上的视图的绑定。列表中的每个视图在被单击时都将更新这个最新绑定中的视图。
相反,将binding作为setPrice函数的另一个参数。将binding作为getView()中的局部变量,而不是属性。
convertView不为空时,你也应该使用它,这样你的视图就可以被循环使用。这有助于保持动画在滚动时的流畅性。所以把你的代码改为:
第一次
顺便说一句,我不知道你的override fun getItem(position: Int): Any函数是从哪里来的。它不是Android BaseAdapter类的一部分。但看起来它做错了事情。它不应该返回一个PizzaItemList项,而不是传入的位置吗?

相关问题