Checkbox ListView

This small chuck of code saves time and effort if you’re looking for a solution to implementing a ListView that supports Checkboxes.

The end result:

I initialized my list inside onCreate( )
ListView listView1;

// Populate this string array to create ListView items
String[] listItems = { "List Item #1", "List Item #2" };

// Initialize ListView
listView1 = (ListView)findViewById(R.id.listView);
listView1.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, listItems));
listView1.setItemsCanFocus(false);
listView1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

// Check or Uncheck the box onClick
listView1.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
      CheckedTextView ctv = (CheckedTextView)arg1;

      // Any additional stuff to do goes here
   }
});