【问题标题】:checkbox's in a listView / adapter are "pressed" on their ownlistView / 适配器中的复选框被自己“按下”
【发布时间】:2017-03-19 02:21:44
【问题描述】:

更新 #2 我有一个 listView 和一个适配器。 listView 中的每个项目都有一个复选框。 当单击列表视图中的复选框时,下面的复选框 5-6 项(重复 - 所有复选框)也似乎被按下 - 灯亮着,即使没有人点击,当检查它是否实际按下时'isChecked' bool 标志,为假。

公共类 watchAllAdapter 扩展 ArrayAdapter {

public watchAllAdapter(Context context, ArrayList<Subject> arrayList) {
    super(context, 0, arrayList);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.for_each_watch_all, parent, false);
    }
    final Subject currSubject = getItem(position);

    final CheckBox cb = (CheckBox) listItemView.findViewById(R.id.checkbox_for_each);
    cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
            if (isChecked){
                currSubject.setChosen(true);
            }
            else{
                cb.setChecked(false);
                currSubject.setChosen(false);
            }
        }
    });

    return listItemView;
}

谢谢=]

【问题讨论】:

  • 请在下方发布您的代码,以便我们检查
  • 做到了,感谢您的回复,新人=]。

标签: java android listview checkbox adapter


【解决方案1】:

如果我是你,我建议只使用...

if(cb.isChecked()) {
    currSubject.setChosen(true);
}
else if (!cb.isChecked()) {
    currSubject.setChosen(false);
}

【讨论】:

  • 以及checkedunchecked复选框的情况将如何处理??
【解决方案2】:

尝试遵循 View Holder 模式。创建一个 Holder 类并在视图不为空时重用该视图。如果视图已经存在,还将复选框的 onCheckedChangedListener() 设置为 null。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View listItemView = convertView;
    Holder holder = null;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.for_each_watch_all, parent, false);
        holder = new Holder();

        holder.ckbox =(CheckBox)row.findViewById(R.id.check_box_id);

        listItemView .setTag(holder);
    }else {

        holder = (Holder) listItemView.getTag();
        holder.ckbox.setOnCheckedChangeListener(null);

    }
    final Subject currSubject = getItem(position);

    final CheckBox cb = (CheckBox) listItemView.findViewById(R.id.checkbox_for_each);
    cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
            if (isChecked){
                currSubject.setChosen(true);
            }
            else{
                cb.setChecked(false);
                currSubject.setChosen(false);
            }
        }
    });

    return listItemView;
}

private class ViewHolder {
    CheckBox checkBox;
}

【讨论】:

  • 在你的 sol 的第 9 行中,“行”是谁?我可以使用 listItemView 访问 R.id .......在此“如果”中为 null ...,编辑:谁是“R.id.check_box_id”?
  • 用listItemView替换
  • 这样做了,同样的问题仍然存在。
  • 从这里使用您的解决方案和“josephus”解决了问题:stackoverflow.com/questions/16350670/…,谢谢!!
猜你喜欢
  • 2014-05-20
  • 1970-01-01
  • 2013-01-28
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
  • 2014-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多