这里是,当我选中复选框时,获取数据并用 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/