How to Create Multiple Choice List

This Android tutorial demonstrates how to create multiple choice list and handle the item click.


android multiple choice list

The steps for creating the application are:

Create the Android Project. Select File>New>Android Project in the Eclipse menu.

Create a class that extends the ListActivity.

public class MainActivity extends ListActivity { }

Create an array of Strings

private String[] colors = {"Red","Blue","Green","Black","White"};

Create an ArrayAdapter. Use android's built-in layout android.R.layout.simple_list_item_multiple_choice for multiple choice.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,colors);

Get the activity's ListView and set its choice mode as multiple choice.

getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

Set the List Adapter.

setListAdapter(adapter);

Implement/override the onListItemClick() method. This method is called whenever an item from the list is selected.
To access the selected items use ListView.getCheckedItemPositions() method. This method returns the set of checked items in the list.
If the adapter has stable IDs you can also use ListView.getCheckedItemIds() method . This method returns the set of checked items ids.

protected void onListItemClick(ListView l, View v, int position, long id) {
  // TODO Auto-generated method stub
  super.onListItemClick(l, v, position, id);

  Toast.makeText(this,"Item Clicked", Toast.LENGTH_SHORT).show();
}

Final code for the MainActivity.java:

public class MainActivity extends ListActivity {

 private String[] colors = {"Red","Blue","Green","Black","White"};

  /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
       
         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,colors);
         // Get the activity's ListView and set its choice mode as multiple choice
         getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
         setListAdapter(adapter);
     }

  protected void onListItemClick(ListView l, View v, int position, long id) {
   // TODO Auto-generated method stub
   super.onListItemClick(l, v, position, id);

   Toast.makeText(this,"Item Clicked", Toast.LENGTH_SHORT).show();
  
  }
}

Conclusion:

In this example we learn how to create multiple choice list of items and handling on list item click event.

Comments

I'm quite pleased with the infoarmotin in this one. TY!

what if the adapter came from simplecursoradapter? how do we code that? to have multiple choice..??

Read this tutorial.

http://www.mubasheralam.com/tutorials/android/listview-using-cursor-and-simplecursoradapter

and on this line of code:

adapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1 ,c , columns , to);

replace:  android.R.layout.simple_list_item_1

with

android.R.layout.simple_list_item_multiple_choice

and in the next line write follwoing line of code

getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

 

Big help, big help. And supelrative news of course.

And I was just wodnreing about that too!

Now I feel sutipd. That's cleared it up for me

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.