Android: ListView CheckBox checked unintentionally

痞子三分冷 提交于 2020-01-07 06:40:15

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/6048339/android-listview-checkbox-checked-unintentionally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!