Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a listView with checkBox. When checkBox is checked/unchecked, it will count and display the number in `button` `delete` setText. If the counter is equal to 0, then it will only display `DELETE`, otherwise it will display `DELETE (with the counter)`. But the problem is the delete still setText `DELETE (with the counter)` although the counter is 0.

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


What I have tried:

The above code is what I have tried, but no luck
Posted
Updated 21-Nov-16 18:12pm
v2
Comments
Richard MacCutchan 21-Nov-16 13:14pm    
if (checkBoxCounter > 0)
...
wseng 21-Nov-16 23:48pm    
where should I put the if block?
Richard MacCutchan 22-Nov-16 2:36am    
Put it round the statement that you only want to appear when the value is greater than 0. This is basic Java, nothing complicated.
[no name] 21-Nov-16 13:40pm    
if(checkBoxCounter.equals(0))
wseng 21-Nov-16 23:41pm    
cannot resolve equals

1 solution

I solved it using this answer

Java
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 + ")");
                      }
                  }
              }
          });
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900