jeudi 21 janvier 2016

on select checkbox outside the listview all checkboxes inside the listview should get checked

in my fragment i have listview and checkbox outside listview, in each row of listview i have some textview and checkbox, when i select the checkbox outside the listview all checkbox in the listview in each row should get checked and when uncheked all should uncheck , but problem is that only visible checkbox are getting checked insted all, and on scroll checked ones are getting unchecked. i have written this code in oncreateview() method.

here is my code..

checkallorders = (CheckBox) view.findViewById(R.id.checkallorders);
    checkallorders.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            {

                if(mAdapter!=null)
                {
                final ListView listView =  mListView;
                for (int i = 0; i < mAdapter.getCount(); i++) {
                    View view = getViewByPosition(i, listView);
                    CheckBox  cb = (CheckBox) view.findViewById(R.id.checkorder);
                    if (checkallorders.isChecked())
                         cb.setChecked(true);
                        else
                      cb.setChecked(false);}

                }
        }
        }

    });
    return view;
}

public View getViewByPosition(int position, ListView listView) {
   final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

    if (position < firstListItemPosition || position > lastListItemPosition ) {
        return listView.getAdapter().getView(position, listView.getChildAt(position), listView);
    } else {
        final int childIndex = position - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}




Aucun commentaire:

Enregistrer un commentaire