2

I have a Checkbox in a ListView to select items and I have one Button outside the ListView. Initially the Checkbox should be hidden, when I click that Button the Checkbox should display in the ListView and vice versa.

I have one issue in that, when I press the Button initially it displays one Checkbox and again I press the Button it show a few checkboxes but what I want was initially it should be invisible when I press the Button it should be visible in ListView

Note: I have a Button in class and Checkbox in adapter

sdel.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

            ((datalist) mlistView.getAdapter()). toggleChecks();
            ((datalist)mlistView.getAdapter()).notifyDataSetChanged();
    }

});    
public void toggleChecks() { 

   for (int i = 0;i<sms.size();i++) {
        holder.cb.setVisibility(CheckBox.VISIBLE);
   } 

   isCheckBoxVisible=!isCheckBoxVisible;
   notifyDataSetChanged();
}
Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
ABDUL RAHMAN
  • 85
  • 13
  • Your question is not clear. Can you explain it simply ? – madhan kumar Jul 04 '16 at 06:13
  • i have one fragment class and adapter class in that adapter i set one checkbox to select/deselect and i have one button in class (outside the listview).initially the checkbox should be in hide state and when i click the button it displays ...simple @ 0X0nosugar – ABDUL RAHMAN Jul 04 '16 at 06:44

1 Answers1

4

Create model for this purpose in that define getter setter for boolean variable

private  boolean isVisible;

public boolean isVisible() {
    return isVisible;
}

public void setVisible(boolean visible) {
    isVisible = visible;
}

in activty set this flag as false by default

YourModel model=new YouModel();
 for (int i = 0; i <sms ; i++) {

        model.setVisible(false);
    }

In adapter write condition for check box //assuming sms is arraylist

YourModel model=sms.get(position)
if(model.isVisible){
checkbox.setVisibility(View.VISIBLE);
}else{
checkbox.setVisibility(View.INVISIBLE);
}

now on button click write below code //if checkbox is visible

  for (int i = 0; i <sms.size() ; i++) {
            sms.get(i).setVisible(false);
        }
adapter.notifyDataSetChanged();

//if checkbox is invisible
  for (int i = 0; i <sms.size() ; i++) {
            sms.get(i).setVisible(true);
        }
adapter.notifyDataSetChanged();
Jinal Patel
  • 699
  • 5
  • 15