2017-05-18 69 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; 
} 

};

相關問題