Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 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 Reply

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

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...