Para resolver este problema, tendrá que crear una clase personalizada de artículos que representará sus casillas individuales en la lista. El conjunto de estos elementos será utilizado por la clase de adaptador para mostrar sus casillas de verificación.
Esta clase de elemento tendrá una variable booleana isSelected que determinará si la casilla de verificación está seleccionada o no. Usted tendrá que ajustar el valor de esta variable en el método OnClick de su clase de adaptador personalizado
Por ejemplo
class CheckBoxItem{
boolean isSelected;
public void setSelected(boolean val) {
this.isSelected = val;
}
boolean isSelected(){
return isSelected;
}
}
para su clase CustomAdapter que parecen siguiente:
public class ItemsAdapter extends ArrayAdapter implements OnClickListener {
// you will have to initialize below in the constructor
CheckBoxItem items[];
// You will have to create your check boxes here and set the position of your check box
/// with help of setTag method of View as defined in below method, you will use this later // in your onClick method
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
CheckBox cBox = null;
if (v == null) {
LayoutInflater vi = (LayoutInflater) apUI.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.attachphoto, null);
}
CheckBoxItemItem it = items[position];
cBox =(CheckBox) v.findViewById(R.id.apCheckBox);
cBox.setOnClickListener(this);
cBox.setTag(""+position);
Log.d(TAG, " CHECK BOX IS: "+cBox+ " Check Box selected Value: "+cBox.isChecked()+" Selection: "+it.isSelected());
if(cBox != null){
cBox.setText("");
cBox.setChecked(it.isSelected());
}
return v;
}
public void onClick(View v) {
CheckBox cBox =(CheckBox) v.findViewById(R.id.apCheckBox);
int position = Integer.parseInt((String) v.getTag());
Log.d(TAG, "CLicked ..."+cBox.isChecked());
items[position].setSelected(cBox.isChecked());
}
}
Más tarde will declarará y organizará su clase CheckBoxItem que será contenida por su clase Adapter, en este caso será una clase ItemsAdapter.
Luego, cuando el usuario presiona el botón, puede recorrer todos los elementos de la matriz y verificar cuál se selecciona utilizando el método isSelected() de la clase CheckBoxItem.
En su actividad que tendrá:
ArrayList getSelectedItems(){
ArrayList selectedItems = new ArrayList();
int size = items.length;
for(int i = 0; i<size; i++){
CheckBoxItem cItem = items[i];
if(cItem.isSelected()){
selectedItems.add(cItem);
}
}
return selectedItems;
}
http://stackoverflow.com/questions/10895763/checkbox-unchecked-when-i-scroll-listview-in-android/10896140#comment14205583_10896140 –