Hide and show checkbox issue in android

女生的网名这么多〃 提交于 2019-12-23 00:26:33

问题


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();
}

回答1:


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();


来源:https://stackoverflow.com/questions/38177900/hide-and-show-checkbox-issue-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!