4

I have a listview, with around 200 items, I have implemented a custom ArrayAdapter for the checkboxes. I use a SparseBooleanArray to store the value of the boxes.

All of this works fine but I cannot seem to graphically update the checking of the boxes. If the user clicks, then the box is checked. But, if I call setChecked in my code, it has no impact on the box itself (so, even if its value is true, then it is not ticked).

I have tested by trying to set all boxes to true and no luck, even though they are all checked, they do not show that they are. Not sure what code you need to see but I have chucked in my Adapter for good measure:

class CheckBoxArrayAdapter extends ArrayAdapter<String> implements CompoundButton.OnCheckedChangeListener { 
private SparseBooleanArray checkedBoxes; 
Context context;

public CheckBoxArrayAdapter(Context context, int resource, List<String> objects) { 
    super(context, resource, objects); 
    checkedBoxes = new SparseBooleanArray();
    this.context = context;
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    final CheckBox view = (CheckBox) super.getView(position, convertView, parent);
        //if(convertView == null) {
            //convertView = view.inflate(context, R.layout.list_item, null);
        //}
    view.setTag(position);
    view.setChecked(checkedBoxes.get(position, false));
    System.out.println(view.getTag() + ": " + view.isChecked());
    view.setOnCheckedChangeListener(this); 
    return view; 
} 

public boolean isChecked(int position) { 
    return checkedBoxes.get(position, false); 
} 
public void setChecked(int position, boolean isChecked) { 
    checkedBoxes.put(position, isChecked); 
    notifyDataSetChanged(); 
} 

public void toggle(int position) { 
    setChecked(position, !isChecked(position)); 
} 

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
    checkedBoxes.put((Integer) buttonView.getTag(), buttonView.isChecked()); 
} 

}

Thanks guys!

Christopher Gwilliams
  • 1,321
  • 15
  • 29
  • May not be worth mentioning, but even setting the checkbox to be enabled in the XML showed nothing, sure I am missing something minor but I cannot see it! – Christopher Gwilliams Apr 06 '11 at 16:58
  • Out of curiosity, why not just use the built in support for multi select in ListView? http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:choiceMode – Cheryl Simon Apr 06 '11 at 17:07
  • I have added the android:choiceMode to be multiple choice but it seemed like the general consensus was to use a custom adapter. The majority of android apps seemed to incorporate checkboxes. – Christopher Gwilliams Apr 06 '11 at 17:24
  • The general consensus where? In what context? I don't think those are mutually exclusive. The point is just that you shouldn't need to be tracking the checked state yourself, you should be able to just say ask for the checked items via http://developer.android.com/reference/android/widget/AbsListView.html#getCheckedItemIds() – Cheryl Simon Apr 07 '11 at 00:16

1 Answers1

2

Ok, seems that Checked Text View is the best way of doing it, use them as the list item and this as your listener:

            public void onItemClick(AdapterView<?> parent, View view, int pos,long id) 
        {
            if(!isChecked.get(pos)) {
                messageList.setItemChecked(pos, true);
                isChecked.put(pos, true);
            }else{
                messageList.setItemChecked(pos, false);
                isChecked.put(pos, false);
            }
            //sendEmail("encima+Gmail4wrdr@gmail.com", address.get(pos) + ": " +  subject.get(pos), Jsoup.parse(message.get(pos)).toString());
        }   

The example I used previously was given by a Google Android Dev, that is why I thought it was commonplace. But, whether it is or not, this way is much simpler!

Christopher Gwilliams
  • 1,321
  • 15
  • 29