这是当我选中复选框时,获取数据并显示Toast。但是当我点击Restart按钮时如何取消选中复选框?有人可以指我取消选中复选框吗?
这是我的产品
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;
}
答案 0 :(得分:1)
尝试这个。
editInventoryItem
答案 1 :(得分:0)
将Arraylist声明为成员变量
ArrayList box = new ArrayList();
//在适配器中添加此方法以取消选中复选框
pubic void uncheckAll(){
for(Product p:box)
{
p.box = FALSE;
}
noifyDataSetChanged();
}
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
box.get((Integer) buttonView.getTag()) .box =isChecked;
}
};