Recipe Implementing a List of Choices

A common situation in applications is to provide a user with a list of choices that can be selected by clicking them.This can be easily implemented utilizing ListActivity, a subclass of Activity, and triggering an event based on what choice was made. The steps for creating a list of choices are

1. Create a class that extends the ListActivity class instead of the Activity class: public class ActivityExample extends ListActivity {

//content here

2. Create a String array of labels for each choice:

static final String[] ACTIVITY_CHOICES = new String[] { "Action 1", "Action 2", "Action 3"

3. Call setListAdapter() with the ArrayAdapter specifying this list and a layout: setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ACTIVITY_CHOICES)); getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); getListView().setTextFilterEnabled(true);

4. Launch an OnItemClickListener to determine which choice was selected and act accordingly:

getListView().setOnItemClickListener(new OnItemClickListener() {

@Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { switch(arg2) {//extend switch to as many as needed case 0:

//code for action 1

break; case 1:

//code for action 2 break; case 2:

//code for action 3 break;

default: break; }

This technique is utilized in the next recipe.

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook

Post a comment