android - 如何取消选中 CheckBox 中的选中项?

标签 android checkbox

这里是,当我选中复选框时,获取数据并用 Toast 显示。但是,当我单击“重新启动”按钮时如何取消选中复选框?有人可以指点我取消选中复选框吗?

这是我的产品

public class Product {
    String name;
      boolean box;    
}

这是我的 ListAdapter

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        view = lInflater.inflate(R.layout.item, parent, false);
    }

    Product p = getProduct(position);    
    ((TextView) view.findViewById(R.id.tvDescr)).setText(p.name);  
    CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox);
    cbBuy.setOnCheckedChangeListener(myCheckChangList);
    cbBuy.setTag(position);
    cbBuy.setChecked(p.box);
    return view;
}

Product getProduct(int position) {
    return ((Product) getItem(position));
}

ArrayList<Product> getBox() {
    ArrayList<Product> box = new ArrayList<Product>();
    for (Product p : objects) {
        if (p.box)
            box.add(p);
    }
    return box;
}

OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
        getProduct((Integer) buttonView.getTag()).box = isChecked;
    }
};

这是我的 MainActivity

    case R.id.restart:
      for (Product p : boxAdapter.getBox()) {
      String result = "Selected Product are :";
       if (p.box){ // if check
        result += "\n" + p.name;
       }
      }
      return true;
    }

最佳答案

试试这个。

case R.id.restart:
  for (Product p : boxAdapter.getBox()) {
  String result = "Selected Product are :";
   if (p.box){ // if check
    result += "\n" + p.name;
   }
     p.box=false;
  }
  adapter.notifyDataSetChanged();
  return true;
}

关于android - 如何取消选中 CheckBox 中的选中项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44039200/

相关文章:

javascript - 复选框被选中但不执行功能

javascript - 显示 View 后触发点击时 Switchery 呈现错误

css - 使用 CSS 隐藏特定的复选框

Android imagebutton 在点击时不闪烁

javascript - 在不重新加载页面的情况下更改 Android Webview 哈希

android - 无法解析符号 AppCompatActivity

Android 计费 - 在文件 Security.java 中,base64EncodedPublicKey 应该是编码值吗?

android - 是否可以在 Python 3 上为 Android 构建 Kivy 应用程序?

php - 如果未选中,则尝试设置默认复选框值

delphi - TListView 标题列中的复选框 - 如何防止它窃取焦点?