Reset counter after pressing a button

In DeleteTask , I have a button used to delete a list if the checkbox is checked in the View list.

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

In the DeleteAdapter, it has a counter used to count the checked flag and display the counter in the button . After pressing the delete button counter assumes that reset is 0 and shows only DELETE in the delete button setText.

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

Now my problem is when the delete button is pressed, it shows "DELETE", but when I check the checkbox, the counter does not reset. How to reset the counter?

Edit

I change the counter to checkBoxCounter, but still get the same result!

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

When I press button delete, the button shows DELETE . But, when I click another checkbox again, the counter starts from the last set instead of 1.

Please, help.

This is part of the screen of my application.

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

+8
android checkbox listview counter
source share
3 answers

If there are two different classes, add the method inside the adapter class to the reset counter.

Maybe so:

  • Inside the adapter class add:

     public void resetCheckedCounter(){ checkBoxCounter = 0; } 
  • For the Delete button, add:

     adapter.resetCheckedCounter(); 

Hope this help!

+9
source share

assign checkBoxCounter = 0 Inside the onClick () function. and for security reasons, you can change the code below to avoid updating checkBoxCounter from any other part of the code.

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

Instead of counter = 0 you need to do checkBoxCounter = 0 inside delete click listener

+2
source share

All Articles