按钮显示复选框计数器

时间:2016-11-22 04:54:40

标签: android listview checkbox

我有一个带checkBox的listView。选中/取消选中复选框后,它将计算并显示button delete setText中的数字。如果计数器等于0,则它仅显示DELETE,否则将显示DELETE (with the counter)。但问题是删除仍然是setText DELETE (with the counter),尽管计数器为0。

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
                        {
                            if(checkBoxCounter==0)
                            {
                                delete.setText("DELETE");
                            }
                            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--;
                                delete.setText("DELETE" + "" + "(" + checkBoxCounter + ")");
                            }
                        }
                    }
                });

2 个答案:

答案 0 :(得分:1)

将您的代码更改为此

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
                    {
                        checkBoxCounter--;
                        if(checkBoxCounter==0)
                        {
                            delete.setText("DELETE");
                        }
                        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.
                            delete.setText("DELETE" + "" + "(" + checkBoxCounter + ")");
                        }
                    }
                }
            });

希望这适合你!

答案 1 :(得分:1)

现在你永远不会检查0.将底部的其他块更改为:

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 + ")");
   }
}

并删除之前的检查