2017-05-18 32 views
1

这里是,当我检查复选框时,获取数据和吐司显示。但是当我点击重新启动按钮时如何取消选中复选框?有人可以指示我取消选中复选框吗?如何取消勾选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; 
    } 

回答

1

试试这个。

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; 
} 
+0

它工作正常。谢谢@Vasant –

+0

欢迎... @Tr.Buu – Vasant

0

申报的ArrayList作为成员变量

ArrayList的盒=新的ArrayList();

//在适配器添加这种方法来取消选中复选框

耻骨空隙uncheckAll(){

为(产品号码:盒)

{

p.box = FALSE;

}

noifyDataSetChanged();

}

OnCheckedChangeListener myCheckChangList =新OnCheckedChangeListener(){

public void onCheckedChanged(CompoundButton buttonView, 
     boolean isChecked) { 
    box.get((Integer) buttonView.getTag()) .box =isChecked; 
} 

};

相关问题