简体   繁体   中英

Android: ListView CheckBox checked unintentionally

I have a ListView which is layout below a header bar and layout above a footer bar. Each row of the ListView contains a LinearLayout with a CheckBox and TextView side by side. Now, when I have more rows that can be shown at a time, you can of course scroll down or up. However, once I scroll down or up the first and / or last CheckBox is being checked automatically. My finger is nowhere near where you would select the check box. What could be causing this? Note I used code here: http://www.anddev.org/checkbox_text_list___extension_of_iconified_text_tutorial-t771.html

Looking briefly into code presented under link in question I can see the following:

 public View getView(int position, View convertView, ViewGroup parent){
      CheckBoxifiedTextView btv;
      if (convertView == null) {
           btv = new CheckBoxifiedTextView(mContext, mItems.get(position));
      } else { // Reuse/Overwrite the View passed
           // We are assuming(!) that it is castable!
           CheckBoxifiedText src = mItems.get(position);
           btv = (CheckBoxifiedTextView) convertView;
           btv.setCheckBoxState(src.getChecked());  // set checked state
           btv = (CheckBoxifiedTextView) convertView;
           btv.setText(mItems.get(position).getText());
      }
      return btv;
 }

As you can see the view is reused during scroll and checkbox state is set with method setCheckBoxState . Then in CheckBoxifiedTextView you can find:

 public void setCheckBoxState(boolean bool)
 {
     mCheckBox.setChecked(mCheckBoxText.getChecked());
     mCheckBoxText.setChecked(true); // <-- HERE !
 } 

where in 4th line there is setChecked( true ) hardcoded which might be causing the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM