How to Create Single Choice List

This Android tutorial demonstrates how to create a single choice list and handle the item clicked.

Android single choice list

The steps for creating the applicaion 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_single_choice for single choice.

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

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

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

Set the List Adapter.

setListAdapter(adapter);

Now itemImplement/override the onListItemClick() method. This method is called whenever an item from the list is selected.
Next, to get the data associated with the slected item call getListView.getItemAtPosition(position) method. Finally, display the text of selected item as a Toast message.

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

  // Get the data associated with selected item
  Object item = l.getItemAtPosition(position);

  // Display data/text of the item/row clicked
  Toast.makeText(this,"Selection: "+ item.toString(), 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_single_choice,colors);
        // Get the activity's ListView and set its choice mode as single choice
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        setListAdapter(adapter);
    }

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  // TODO Auto-generated method stub
  super.onListItemClick(l, v, position, id);
  
  // Get the data associated with selected item
  Object item = l.getItemAtPosition(position);
  // Display data/text of the item/row clicked
  Toast.makeText(this,"Selection: "+ item.toString(), Toast.LENGTH_SHORT).show();
 }
}

Conclusion:

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

Comments

thank you

the code works

Going to put this atrcile to good use now.

AFAIC that's the best ansewr so far!

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.