In DeleteTask, I have a button used to delete the list if checkbox is checked in the 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;
}
}
}
});
In DeleteAdapter, it has a counter used to count the checked box and display the counter in button
. Once the delete button
is clicked, the counter suppose reset to 0 and only shows DELETE
in 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 + ")");
}
}
}
});
My problem now is when the delete button is clicked, it show 'DELETE', but when I check the checkBox, the counter did not reset. How to reset the counter ?
Edit
I have change the counter to checkBoxCounter, but still getting 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 click
button
delete, the button showsDELETE
. But, when I press another checkbox again, the counter starts from the last set value instead of 1.
Please help.
Here are part of my app screen shot
https://i.stack.imgur.com/R954N.jpg
If there are two different classes, then add a method inside the adapter class to reset the counter.
May be like this:
Inside adapter class, add:
public void resetCheckedCounter(){
checkBoxCounter = 0;
}
For the "Delete" button, add:
adapter.resetCheckedCounter();
Hope this help!
assign checkBoxCounter = 0
Inside onClick() function.
and for safety purpose you can change code as bellow to avoid updating checkBoxCounter from some 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 + ")");
}
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With