3

I have read a lot of threads here about listviews and checkboxes. Lots of them use a CheckedTextView or extend it. I want to implement a custom listview with a checkbox behaviour like on the android mail apps (Gingerbread, ICS): There only checkboxes are checkable and not the whole row. Plus on ICS the actionbar indicates the number of checked list items.

Can anyone please show me some code or point me in the right direction? Thanks!

felix
  • 123
  • 2
  • 7

1 Answers1

4

Checkout out the sample in API Demos List 16 Multi selection mode

public class List16 extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ListView lv = getListView();
    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    lv.setMultiChoiceModeListener(new ModeCallback());
    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_activated_1, 
            Cheeses.sCheeseStrings));
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    getActionBar().setSubtitle("Long press to start selection");
}

private class ModeCallback implements ListView.MultiChoiceModeListener {

    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.list_select_menu, menu);
        mode.setTitle("Select Items");
        return true;
    }

    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return true;
    }

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
        case R.id.share:
            Toast.makeText(List16.this, "Shared " + getListView().
            getCheckedItemCount() +
                    " items", Toast.LENGTH_SHORT).show();
            mode.finish();
            break;
        default:
            Toast.makeText(List16.this, "Clicked " + item.getTitle(),
                    Toast.LENGTH_SHORT).show();
            break;
        }
        return true;
    }

    public void onDestroyActionMode(ActionMode mode) {
    }

    public void onItemCheckedStateChanged(ActionMode mode,
            int position, long id, boolean checked) {
        final int checkedCount = getListView().getCheckedItemCount();
        switch (checkedCount) {
            case 0:
                mode.setSubtitle(null);
                break;
            case 1:
                mode.setSubtitle("One item selected");
                break;
            default:
                mode.setSubtitle("" + checkedCount + " items selected");
                break;
        }
    }

}
}
Rajdeep Dua
  • 11,190
  • 2
  • 32
  • 22
  • 3
    thanks for your reply! Is there any chance to implement that behaviour on pre Honeycomb devices? – felix Nov 22 '11 at 10:19
  • Gmail listview able to multiple check and when in multiple check, it can click to show the detail. List16 demo still not solve the problem – uudashr Aug 05 '13 at 01:53