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
android checkbox listview counter
Hoo
source share