AlertDialog with a List of Items

Tutorial. I was able to give the dialog an icon and a title. But when I gave it a message, the list of items disappeared.

Source code in ListDialog.zip

  1. ListDialogActivity.java
  2. R.java
  3. main.xml
  4. AndroidManifest.xml

AlertDialog with a list of items in ApiDemos/App/Alert Dialogs

Source code is in AlertDialogSamples.java, alert_dialog.xml, and strings.xml.

  1. List dialog: case DIALOG_LIST. Shows a little dialog with a message when you click on an item.
  2. Single choice list: case DIALOG_SINGLE_CHOICE. Radio buttons with Cancel and OK at the bottom.
  3. Repeat alarm: case DIALOG_MULTIPLE_CHOICE. Check boxes with Cancel and OK at the bottom.

Things to try

  1. Give each item a radio button. Add the following fields to class ListDialogActivity
    	static final int checkedItem = 3;	//Rearden metal is already checked.
    	int which = checkedItem;		//which item is currently checked
    
    Change the call to setItems to the following. See ApiDemos/App/Alert Dialogs/Single choice list.
    			builder.setSingleChoiceItems(items, checkedItem, new DialogInterface.OnClickListener() {
    				@Override
    				public void onClick(DialogInterface dialog, int which) {
    					((ListDialogActivity)((Dialog)dialog).getOwnerActivity()).which = which;
    					textView.setText("Thank you for considering " + items[which] + ".");
    				}
    			});
    
    			builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    				@Override
    				public void onClick(DialogInterface dialog, int which) {
    					textView.setText("");
    				}
    			});
    
    			builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    				@Override
    				public void onClick(DialogInterface dialog, int which) {
    					int i = ((ListDialogActivity)((Dialog)dialog).getOwnerActivity()).which;
    					textView.setText("Thank you for selecting " + items[i] + ".");
    				}
    			});
    

  2. Give each item a checkbox (with a green check) instead of a radio button. See ApiDemos/App/Alert Dialogs/Repeat alarm. Change the call to setSingleChoiceItems to the following. You’ll also have to replacethe which method of the activity with an array of booleans.
    		boolean[] checkedItems = {true, false, false, true};
    
    			builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
    				@Override
    				public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    					if (isChecked) {
    						((ListDialogActivity)((Dialog)dialog).getOwnerActivity()).which = which;
    						textView.setText("Thank you for considering " + items[which] + ".");
    					} else {
    						textView.setText("");
    					}
    				}
    			});