I have been reading tutorials and forum posts for the past few hours to try and get a simple checkbox listview implemented.
I understand (i believe) how I need to maintain the state of my checkboxes in a list (o in my case a map).
It is such a simple thing I am sure but my checkbox is not being set to true or false in my bindView method. Here is the code
public void bindView(View view, Context context, Cursor cursor) {
final int rowId = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
familyText = (TextView)view.findViewById(R.id.contacts_row_family_name);
markedBox = (CheckBox)view.findViewById(R.id.contacts_row_check);
familyText.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
view.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
Log.d("OnClick","Row clicked");
boolean currentlyChecked;
if(checkedState.size() >= rowId){
currentlyChecked = checkedState.get(rowId);
checkedState.put(rowId, !currentlyChecked);
}else{
currentlyChecked = false;
checkedState.put(rowId, !currentlyChecked);
}
markedBox.setChecked(checkedState.get(rowId));
}
});
setProgressBarIndeterminateVisibility(false);
}
Here is the declaration of checkedState. This is class member of my Activity.
private Map<Integer, Boolean> checkedState = new HashMap<Integer,Boolean>();
As far as I can make out I should have a listener on the row (this works because my Log message prints correctly) but the Checkbox doesn’t change.
Update:
I was incorrect that my checkboxes weren’t being checked. Just the wrong one is being checked. I will close this question.