2012-12-02 35 views
0

我正試圖通過一個按鈕單擊通過自定義適配器來檢查所有複選框。但是,當我執行在自定義適配器中檢查所有複選框的方法

ArrayList<BazarItems> objects 

    public void allChecked(){ 

     for (int i=0; i<objects.size(); i++){ 

      cbBuy.setChecked(true); 
     } 

    } 

唯一的最後一項被檢查。 我在做什麼錯?

已經試過這一個

`public void checkAll() { 

     for (int i=0; i<objects.size(); i++){ 

      BazarItems b = objects.get(i); 

      b.box = true; 

      cbBuy.setChecked(b.box); 
     } 
    } 
    ` 

在這裏我們去。我可能在自定義適配器上缺少一些東西。根據我的理解,我所擁有的應該足以完成任務。

`public class BazarListAdapter extends BaseAdapter { 

    Context ctx; 
    LayoutInflater lInflater; 
    ArrayList<BazarItems> objects; 

    CheckBox cbBuy; 

    BazarListAdapter(Context context, ArrayList<BazarItems> products) { 
     ctx = context; 
     objects = products; 
     lInflater = (LayoutInflater) ctx 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public int getCount() { 
     return objects.size(); 
    } 

    public Object getItem(int position) { 
     return objects.get(position); 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 

     View view = convertView; 
     if (view == null) { 
      view = lInflater.inflate(R.layout.activity_bazar_list_child_item, 
        parent, false); 
     } 

     BazarItems i = getItems(position); 

     ((TextView) view.findViewById(R.id.tvDescription)).setText(i.name); 
     ((TextView) view.findViewById(R.id.tvQuantity)) 
       .setText(i.quantity + ""); 
     ((TextView) view.findViewById(R.id.tvPrice)).setText(i.price + ""); 

     cbBuy = (CheckBox) view.findViewById(R.id.checkBox); 

     cbBuy.setOnCheckedChangeListener(myCheckChangList); 

     cbBuy.setTag(position); 

     cbBuy.setChecked(i.box); 
     return view; 
    } 

    BazarItems getItems(int position) { 
     return ((BazarItems) getItem(position)); 
    } 

    ArrayList<BazarItems> getBox() { 
     ArrayList<BazarItems> box = new ArrayList<BazarItems>(); 
     for (BazarItems i : objects) { 
      if (i.box) 
       box.add(i); 
     } 
     return box; 
    } 

    OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() { 
     public void onCheckedChanged(CompoundButton buttonView, 
       boolean isChecked) { 
      getItems((Integer) buttonView.getTag()).box = isChecked; 
     } 
    }; 

`

回答

2

使你的CustomAdapter

boolean isAllCHecked=false; 

和setter方法申請了這個喜歡

public setAllChecked() 
{ 
    isAllCHecked=true; 
    notifyDataSetChanged(); 
} 

,並在您CustomAdaptergetView做這樣

//get instance of the checkbox 
//call checkBox.setChecked(isAllCHecked); 
+0

非常感謝Mohsin。有用。在Android知識的這個階段,我自己找不到解決方案。 – user1706819

0

您在同一對象上始終調用setChecked()。

嘗試:

for (int i=0; i<objects.size(); i++){ 

     objects.get(i).box = true; 
    } 

,然後invalidate();你的ListView。

+0

你的建議,有些事情永遠不會因爲沒有setChecked(boolean)方法BazarItems – user1706819

+0

我試過另一個(見編輯),但結果是一樣的。 – user1706819

+0

對不起,我誤解了。顯示你的CustomAdapter。 – mirekkomis

相關問題