teekib
teekib

Reputation: 2821

Android: uncecking the check box on button click

I'm trying to implement clearing all the check marks of check boxes on single button click, code seems okay for me but it isn't working. Checkboxes is in different layout...in my present activity I'm inflating the checkboxes and doing setChecked(false)(which is not working)..is there any another way?

Button clearbtn1 = (Button) findViewById(R.id.clearbtn);
        clearbtn1.setOnClickListener(new OnClickListener() {
            @Override

            public void onClick(View v) {



                LayoutInflater inflater_example = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
                View v1= inflater_example.inflate(R.layout.profile, null); 
                CheckBox checkBox = (CheckBox) v1.findViewById(R.id.checkBox1);
                checkBox.setChecked(false); 
        }
        });

Any help would be appreciated.

Upvotes: 0

Views: 434

Answers (2)

mango
mango

Reputation: 5636

your adapter has these views that you want to effect. standard OOP principles dictate that you make adapter methods to go about it. judging from your adapter technique and how it deftly combats convertView to persist checkbox states, i'd think the method would be self-evident. simply set the boolean flags that determine their states and refresh the adapter. here's a sample adapter method, that you can call from the button's OnClickListener

private void clearChecks() {
    for (int i = 0; i < Constants.checkBoxState.length; i++) {
        Constants.checkBoxState[i] = false;
    }
    notifyDataSetChanged();
}

Upvotes: 1

user936414
user936414

Reputation: 7634

Just clear all the checkbox states in Constants and notify or reset adapter for the change to reflect.

Button clearbtn1 = (Button) findViewById(R.id.clearbtn);
    clearbtn1.setOnClickListener(new OnClickListener() {
        @Override

        public void onClick(View v) {
          for(int i=0;i < Constants.checkBoxState.length; i++)      
             Constants.checkBoxState[i] = false;

          listAdapter.notifyDataSetChanged();

    }
    });

Upvotes: 1

Related Questions