Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset counter after button click

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 shows DELETE. 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

like image 663
Hoo Avatar asked Nov 22 '16 09:11

Hoo


2 Answers

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

May be like this:

  1. Inside adapter class, add:

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

    adapter.resetCheckedCounter();
    

Hope this help!

like image 188
i_A_mok Avatar answered Oct 16 '22 10:10

i_A_mok


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 + ")");
                        }
                    }
                }
            }); 
like image 4
Swapnil Avatar answered Oct 16 '22 10:10

Swapnil