這裏是,當我檢查複選框時,獲取數據和吐司顯示。但是當我點擊重新啓動按鈕時如何取消選中複選框?有人可以指示我取消選中複選框嗎?如何取消勾選CheckBox中的選中項目?
這裏是我的產品
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;
}
它工作正常。謝謝@Vasant –
歡迎... @Tr.Buu – Vasant