public class MyAdapter extends SimpleAdapter {
Map<Integer, Boolean> map;
LayoutInflater mInflater;
private List<? extends Map<String, ?>> mList;
public MyAdapter(Context context, List<Map<String, String>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
map = new HashMap<Integer, Boolean>();
mInflater = LayoutInflater.from(context);
mList = data;
for (int i = 0; i < data.size(); i++) {
map.put(i, false);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.rapid_diagnosis_row, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.multiple_question);
textView.setText((String) mList.get(position).get("value"));
CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.multiple_checkbox);
checkBox.setChecked(map.get(position));
// save position and checking status into tag
checkBox.setTag(new int[] { position, checkBox.isChecked() == true ? 1 : 0 });
checkBox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// get position and checking status from tag
int[] tag = (int[]) (v.getTag());
int p = tag[0];
int c = tag[1];
// try to get checking status from the clicked view, not works
// which is always true
boolean checked = ((CheckBox) v).isChecked();
// try to toggle the clicked (checkbox) view, not works
// the checkbox pressed never be toggled
((CheckBox) v).toggle();
mSimpleAdapter.map.put(p, (c == 1 ? true : false));
}
});
return convertView;
}