mercredi 17 mai 2017

How to uncheck checked item in CheckBox?

Here is, when I check of checkbox, get data and show with Toast. But how to uncheck of checkbox when I click Restart button ? Can someone point me to uncheck in checkbox?

Here is my Product

public class Product {
    String name;
      boolean box;    
}

Here is my 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;
    }
};

Here is my 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;
    }




Aucun commentaire:

Enregistrer un commentaire