1.)首先创建一个数组,表示适配器中的项目检查状态
(假设您BaseAdapter
为此目的扩展类):
private boolean [] itemsChecked = new boolean [getCount()];
2.)然后创建一个OnCheckedChangeListener
:
private OnCheckedChangeListener listener = new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton button, boolean checked)
{
Integer index = (Integer)button.getTag();
itemsChecked[index] = checked;
}
}
3.)在您的适配器getView()
方法中:
public View getView(int index, View view, ViewGroup parent)
{
/*...*/
CheckBox checkBox = /*get the checkbox*/;
checkbox.setTag(index);
checkBox.setOnCheckedChangeListener(listener);
/*...*/
}
4.)在onClick()
方法中:
public void onClick(View view)
{
//just get the boolean array somehow
boolean [] itemsChecked = adapter.getItemsCheckedArray();
for(int i=0; i<itemsChecked.length; i++)
{
if(itemsChecked[i])
{
//the i th item was checked
}
else
{
//it isnt checked
}
}
}