select all checkboxes of Listview in android

Dubey.A

I'am having Custom dialog in which i've displayed a listView with checkboxes.I want to select all checkBoxes of ListView by Clicking button in dialog.

here is my button onClickListener

selectAll.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                for (int i = 0; i < listView.getChildCount(); i++) {
          LinearLayout myLayout =(LinearLayout)listView.getChildAt(i);
          CheckBox cb = (CheckBox) myLayout.findViewById(R.id.checkbox);
          cb.setChecked(true);
               }
            }
        });

with above code i have can only check those checkboxes which are on view. I know this happens because listView reuses views. please suggest me what to do

vivek mahajan

You can add boolean variable into your model class and set true on your button click listener, check this:

         class YourModel{
            public boolean isSelected() {
                return isSelected;
            }

            public void setSelected(boolean selected) {
                isSelected = selected;
            }
          }

Your button onClick change.

              public void onClick(View view) {
                    for (int i = 0; i < yourArrayList.size(); i++) {
                        yourArrayList.get(i).setSelected(true);
                    }
                    yourAdapter.notifyDataSetChanged();
                }

your adapter getView method

          public View getView ( final int position, View convertView, ViewGroup parent){
             CheckBox cb = (CheckBox) myLayout.findViewById(R.id.checkbox);
            cb.setChecked(yourModel.isSelected());
          }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive