Aby rozwiązać ten problem, należy utworzyć niestandardową klasę pozycji, która będzie reprezentować poszczególne pola wyboru na liście. Tablica tych elementów zostanie następnie użyta przez klasę adaptera do wyświetlenia twoich pól wyboru.
klasa
Pozycja ta będzie mieć logiczną zmienną IsSelected który określi, czy jest zaznaczone pole wyboru, czy nie. trzeba będzie ustawić wartość tej zmiennej w Metodzie OnClick z niestandardowego klasy adaptera
Dla przykładu
class CheckBoxItem{
boolean isSelected;
public void setSelected(boolean val) {
this.isSelected = val;
}
boolean isSelected(){
return isSelected;
}
}
dla swojej klasy CustomAdapter które wyglądają jak następuje:
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());
}
}
później will zadeklaruje i uszereguje klasę CheckBoxItem, która będzie zawarta przez twoją klasę Adapter w tym przypadku będzie to klasa ItemsAdapter.
Następnie, gdy użytkownik naciśnie przycisk, można przejrzeć wszystkie elementy w tablicy i sprawdzić, który z nich jest wybrany przy użyciu metody isSelected() klasy CheckBoxItem.
W swojej działalności trzeba będzie:
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 –