Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

java - Check all checkboxes in a listview when one is clicked

I have a listview whereby each listviewitem has a checkbox that the user can select an item with. I also have a checkbox above the listviewwhich is expected to automatically check all the checkboxes in my list when clicked. I am having problems implementing the appropriate for this. Any ideas?

public class NotesActivity extends AppCompatActivity {
    private CheckBox universalCheckBox;
    private ListView mListNotes;
    NoteListAdapter na;
    private boolean isAnyItemChecked = false;
   private CheckBox mCheckBox;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notes);
        mListNotes = findViewById(R.id.main_listview);
        // handling long click for list view item
         mListNotes.setLongClickable(true);
        mListNotes.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                for (int index = 0; index < parent.getChildCount(); ++index) {
                    View nextChild = (parent.getChildAt(index));
                     mCheckBox = nextChild.findViewById(R.id.checkbox);
                    mCheckBox.setVisibility(View.VISIBLE);
                    universalCheckBox.setVisibility(View.VISIBLE);
                }
                CheckBox checkBox = view.findViewById(R.id.checkbox);
                checkBox.setChecked(true);
                isAnyItemChecked = true;
                return true;
            }
        });
        universalCheckBoxLogic();
    }

    private void universalCheckBoxLogic() {
        universalCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             mCheckBox.setChecked(true); // this not working for me

            }
        });
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
universalCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

       @Override
       public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {         
                        checkAll(isChecked);

       }
   }
);   

     private void checkAll(boolean checked){
               for (int index = 0; index < parent.getChildCount(); ++index) {
                    View nextChild = (listView.getChildAt(index));
                     mCheckBox = nextChild.findViewById(R.id.checkbox);
                    mCheckBox.setVisibility(View.VISIBLE);
                    mCheckBox.setChecked(checked);
                }

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...