Up vote 0 down vote favorite share g+ share fb share tw.
What I do have is a custom row layout for my listview: I am using this layout for my adapter in the activity: adapter = new AdapterCustomBoxes(context, R.layout. Custom_check_row, (ArrayList>) list_values); list. SetAdapter(adapter); list.
SetChoiceMode(ListView. CHOICE_MODE_MULTIPLE); And an adapter for that, here is the getView method of my adapter: public class AdapterCustomBoxes extends ArrayAdapter> { private List> list; private List> orig_list; private Context upper_context; private View view; public AdapterCustomBoxes(Context context, int textViewResourceId, ArrayList> items) { super(context, textViewResourceId, items); this. List = items; this.
Orig_list = items; this. Upper_context = context; } @Override public View getView(int position, View convertView, ViewGroup parent) { view = convertView; if (view == null) { LayoutInflater vi = (LayoutInflater)upper_context. GetSystemService(Context.
LAYOUT_INFLATER_SERVICE); view = vi. Inflate(R.layout. Custom_check_row, null); } Map selectedgroup = new HashMap(); selectedgroup = (Map) list.
Get(position); String itemtext = (String) selectedgroup. Get("item_text"); TextView row_text = (TextView) view. FindViewById(R.id.
Text); row_text. SetText(itemtext); return view; } } And now here comes the point where I get confused. When I check one of those checkboxes the clicked one gets checked.
But without any logic somes of the other line's checkboxes also get checked. And if I then scroll the list the checked status of each box may change from one time I come across this line to the next time. So what is the problem here?
I already tried to do add an onclicklistener to the view in the adapter and then set the explicit checkbox to cheched/unchecked when this listener is triggered but this also does not work as I expected, I clicked on the one box and the other one got checked. So I guess this is a problem with recycled views? Do I have to store the checked Status seperately and restore it every time in the getView method could this be a solution?
Or is there an easier way? Please help ;) So can anybody give me a hint? Thnaks a lot!
--- EDIT --- So what I now tried to save the status of the checkboxes was to creat a map: private Map itm = new HashMap(); and save the status when a box is clicked in in the getView method: CheckBox chkbox = (CheckBox) view. FindViewById(R.id. Chkbox); chkbox.
SetOnCheckedChangeListener(new CheckBox. OnCheckedChangeListener(){ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { itm. Put(buttonView, isChecked); } }); if(itm.
ContainsKey(chkbox)){ iteminfo_row_chkbox. SetChecked( itm. Get(chkbox) ); }else{ iteminfo_row_chkbox.
SetChecked(false); } Sorry if this is the completely wrong approach in any way but shouldn't this work? The result is the same that the whole list and checked statuses are not correct, what am I doing wrong? Android listview checkbox adapter link|improve this question edited Sep 4 '11 at 19:03 asked Sep 4 '11 at 15:03homtg85 17% accept rate.
A few off-topic comments: First you don't need this: Map selectedgroup = new HashMap(); You can just do this: Map selectedgroup = (Map) list. Get(position); Also, are you sure you need the TextView? The checkbox can hold text too.
About your problems, how do you save which checkbox is checked? You should set the checkbox when returning the view, specially when reusing views. -- Edit -- Why don't you create a private class like this?
Private class ListItem{ String text; boolean value; } And then instead of a List> you could just have a List. About the onCheckedChangeListener, you could iterate of the list using the text from the checkbox to find the correct position and then change the value. If the text is not unique, instead of using onCheckedChangeListener, you could use on the ListView a OnItemClickListener which holds the position of the click.
Thanks for the off-topic Stuff! At the moment I do not save the selection status and it seems like that is the problem? So how can I manage to save the status and how do I give this information back to the activity?
Thanks a lot. – homtg Sep 4 '11 at 18:17 see my edit above. – rui.
Araujo Sep 4 '11 at 20:19.
Okay I think I got it, I used a static id value from the selectedgroup map as the key in the itm map to find the checked/unchecked status of the checkbox. Using the view was the wrong approach because they can't be used as the key.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.