单击按钮后重置计数器

时间:2016-11-22 09:55:06

标签: android listview checkbox counter

DeleteTask 中,如果在listView中选中了复选框,我有一个用于删除列表的按钮。

delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int itemCount = listview.getCount();
                for (int i = itemCount - 1; i >= 0; i--) {
                    SearchList search = adapter.getItem(i);
                    if (search.isSelected()) {
                        adapter.removeItem(i);
                        delete.setText("DELETE");
                        counter=0;
                    }
                }
            }
        });

DeleteAdapter 中,它有一个计数器,用于计算复选框并在button中显示计数器。点击删除button后,计数器会重置为0,并且只会在删除DELETE setText中显示button

 holder.ckbox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (((CheckBox) v).isChecked()) {
                        int getPosition = (Integer) v.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                        search.get(getPosition).setSelected(((CheckBox)v).isChecked()); // Set the value of checkbox to maintain its state.
                        checkBoxCounter ++;
                        delete.setText("DELETE"+""+"("+ checkBoxCounter +")");
                    } else
                    {
                        int getPosition = (Integer) v.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                        search.get(getPosition).setSelected(((CheckBox) v).isChecked()); // Set the value of checkbox to maintain its state.
                        checkBoxCounter--;
                        if (checkBoxCounter == 0) {
                            delete.setText("DELETE");
                        }
                        else {
                            delete.setText("DELETE" + "" + "(" + checkBoxCounter + ")");
                        }
                    }
                }
            });

现在我的问题是当点击删除按钮时,它显示'DELETE',但是当我检查checkBox时,计数器没有重置。如何重置计数器?

修改

我已将计数器更改为checkBoxCounter,但仍然得到相同的结果!

 delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int itemCount = listview.getCount();
                for (int i = itemCount - 1; i >= 0; i--) {
                    SearchList search = adapter.getItem(i);
                    if (search.isSelected()) {
                        adapter.removeItem(i);
                        delete.setText("DELETE");
                        checkBoxCounter=0;
                    }
                }
            }
        });
  

点击button删除后,按钮会显示DELETE。但是,当我   再次按另一个复选框,计数器从最后一组开始   价值而不是1。

请帮忙。

以下是我的应用截屏的一部分

https://i.stack.imgur.com/R954N.jpg

3 个答案:

答案 0 :(得分:10)

如果有两个不同的类,则在适配器类中添加一个方法来重置计数器。

可能是这样的:

  1. 在适配器类内,添加:

    public void resetCheckedCounter(){
        checkBoxCounter = 0;
    }
    
  2. 对于"删除"按钮,添加:

    adapter.resetCheckedCounter();
    
  3. 希望这有帮助!

答案 1 :(得分:4)

在onClick()函数内分配checkBoxCounter = 0。 出于安全考虑,您可以将代码更改为以下代码,以避免从代码的其他部分更新checkBoxCounter。

 holder.ckbox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (((CheckBox) v).isChecked()) {
                        int getPosition = (Integer) v.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                        search.get(getPosition).setSelected(((CheckBox)v).isChecked()); // Set the value of checkbox to maintain its state.
                        checkBoxCounter ++;
                        delete.setText("DELETE"+""+"("+ checkBoxCounter +")");
                    } else
                    {
                        int getPosition = (Integer) v.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                        search.get(getPosition).setSelected(((CheckBox) v).isChecked()); // Set the value of checkbox to maintain its state.
                        checkBoxCounter--;
                        if (checkBoxCounter <= 0) {
                            delete.setText("DELETE");
                            checkBoxCounter = 0;
                        }
                        else {
                            delete.setText("DELETE" + "" + "(" + checkBoxCounter + ")");
                        }
                    }
                }
            }); 

答案 2 :(得分:2)

而不是counter = 0您需要在删除点击侦听器

内执行checkBoxCounter = 0