I am new so I may not explain the situation well. Please let me know if you need more info.
I am trying to implement a listview like this:
[textview][button][checkbox]
[textview][button][checkbox]
[textview][button][checkbox]
[textview][button][checkbox]
I want other checkbox unchecked automatically when I click one of them. So it is like a single choice. I write following codes which can implement multiple choice but when I try to switch it to single choice I got trouble.
in checkBoxOnClickListener I cannot find other (checkbox)view and toggle them. Any suggestion pals? Let me know if you think my express skills poor.
Thank you.
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);
}
if (data.get(0).get("value").equals("Male") || data.get(0).get("value").equals("Yes")) {
map.put(1, true);
}
}
@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(position);
checkBox.setOnClickListener(checkBoxOnClickListener);
return convertView;
}
public OnClickListener checkBoxOnClickListener = new OnClickListener() {
public void onClick(View v) {
// ((CheckBox) v).toggle();
boolean checked = ((CheckBox) v).isChecked();
if (checked) {
((CheckBox) v).setChecked(true);
} else {
((CheckBox) v).setChecked(false);
}
int p = (Integer) (v.getTag());
mSimpleAdapter.map.put(p, checked);
// this cannot works, helppp
ListView lv = ((ListView) (v.getParent().getParent()));
(CheckBox) cb = lv.findViewWithTag(1);
cb.toggle();
.....................
……………
}
};
}
Is the problem that
findViewWithTag(1)is returning null? It might be thatfindViewWithTag()does not recursively search for your tag. If you go up parent twice that puts you at yourListViewandfindViewWithTag()will then be looking at the layout objects to be tagged.You should go through each of the
ListView‘s children and then find the check box each one holds. It’s hard to tell from the code snippet here, but why are you using run time set Tags to try to find your check boxes rather than IDs?