我正試圖通過一個按鈕單擊通過自定義適配器來檢查所有複選框。但是,當我執行在自定義適配器中檢查所有複選框的方法
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;
}
};
`
非常感謝Mohsin。有用。在Android知識的這個階段,我自己找不到解決方案。 – user1706819