猿问

如何禁用按钮并清除数组列表?

我有一些按钮和数组列表,我的按钮有条件


if(list.isEmpty){ 

   button.setEnabled(false)  

并将textTotalFinalPrice值设置为0


但就我而言,该功能实际上可以工作,但必须单击两次才能设置值textTotalFinalPrice并禁用button。


如果只点击 1 次ArrayList不清除数据


我已经尝试过了list.clear();还是不行


代码 :


    public void calculateTotalPrice(){

        activity.header.grandTotalPrice();

        activity.textTotalFinalPrice.setText(NumberFormat.getCurrencyInstance(new Locale("id", "id")).format(activity.header.getFinalTotal()));

    }


    private void conditionCartsSize() {

        if (activity.header.getCarts().isEmpty()){

            activity.textFinishOrder.setEnabled(false);

        }else{

            activity.textFinishOrder.setEnabled(true);

            activity.textFinishOrder.setOnClickListener(view -> {

                PrintHelper.bluetoothPrint(activity, activity.header);

                calculateTotalPrice();

                AlertDialog dialog = new AlertDialog.Builder(activity)

                        .setTitle("Pesanan Berhasil")

                        .setMessage("Silahkan Ambil Struk Anda")

                        .create();


                dialog.show();

                activity.header.getCarts().removeAll(activity.header.getCarts());

                notifyDataSetChanged();

            });

        }

    }

我会很感激你的回答


qq_遁去的一_1
浏览 168回答 3
3回答

扬帆大鱼

尝试使用 list = new ArrayList<>();

jeck猫

检查sizeifgetCarts()条件如下&nbsp; private void conditionCartsSize() {&nbsp; &nbsp; &nbsp; &nbsp; if (activity.header.getCarts().size>0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activity.textFinishOrder.setEnabled(false);&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activity.textFinishOrder.setEnabled(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activity.textFinishOrder.setOnClickListener(view -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PrintHelper.bluetoothPrint(activity, activity.header);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; calculateTotalPrice();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AlertDialog dialog = new AlertDialog.Builder(activity)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setTitle("Pesanan Berhasil")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setMessage("Silahkan Ambil Struk Anda")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .create();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dialog.show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activity.header.getCarts().removeAll(activity.header.getCarts());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; notifyDataSetChanged();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

12345678_0001

固定的只需像这样进行一些自定义初始化新的 ArrayList 即可:&nbsp;activity.header = new Header(0,0,0,0,0,0,new ArrayList());
随时随地看视频慕课网APP

相关分类

Java
我要回答