dimanche 11 février 2018

Check all checkboxes on listview from onItemLongClickListener

I am working on a note applications where i am showing check box items on the list views when one is long pressed. The checkboxes are initially visible until when long pressed by the user. The problem is that my method has not been effectively showing the checkboxes on all the items as some are still invisible.

    public class NotesActivity extends AppCompatActivity {
        private ListView mListNotes;
        NoteListAdapter na;
       private CheckBox mCheckBox;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_notes);
            mListNotes = findViewById(R.id.main_listview);
            listViewLongClick();
        }

        private void listViewLongClick() {
            mListNotes.setLongClickable(true);
            mListNotes.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// making checkbox visible on all items
                    for (int index = 0; index < parent.getChildCount(); ++index) {
                        View nextChild = (parent.getChildAt(index));
                         mCheckBox = nextChild.findViewById(R.id.checkbox);
                        mCheckBox.setVisibility(View.VISIBLE);
                    }
// the position long clicked would be automatically checked
                    CheckBox checkBox = view.findViewById(R.id.checkbox);
                    checkBox.setChecked(true);
                    return true;
                }
            });
        }
        @Override
        public void onBackPressed() {
            if(mCheckBox.isShown()) {
    mCheckBox.setVisibility(View.Gone); //i need to remove checkbox visibility here if already visible
            }
            super.onBackPressed();
        }
    }




Aucun commentaire:

Enregistrer un commentaire