This article describes the method of displaying simple and complex lists in AlertDialog in Android. Share it for your reference, as follows:
AlertDialog displays simple list setItems
import ; import ; import ; import ; import ; import ; import ; import ; public class ListDialogTest1extends Activityimplements OnClickListener { private Button btnListDialog; private String[] provinces =new String[] { "Shanghai", "Beijing", "Hunan", "Hubei", "Hainan" }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); btnListDialog = (Button) findViewById(); (this); } @Override public void onClick(View v) { switch (()) { case : showListDialog(); break; } } private void showListDialog() { builder =new (this); ("Please select a province"); /** * 1. public Builder setItems(int itemsId, final OnClickListener * listener) itemsId represents the resource ID of the string array, and the array specified by the resource will be displayed in the list. 2. Public Builder * setItems(CharSequence[] items, final OnClickListener listener) * items represent the string array used to display in the list */ (provinces, new () { @Override public void onClick(DialogInterface dialog,int which) { /* * The ad variable is defined with the final keyword, because the final variable needs to be accessed in the run() method of the implicitly implemented Runnable interface. */ final AlertDialog ad =new ( ).setMessage( "You chose:" + which + ": " + provinces[which]).show(); Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { // Call the dismiss() method of the AlertDialog class to close the dialog box, or you can call the cancel() method. (); } }; // Run the run() method after 5 seconds. (runnable, 5 * 1000); } }); ().show(); } }
AlertDialog Display complex list setMultiChoiceItems
1. The setMultiChoiceItems() method of the class can create a list dialog box., the method is as follows:
1. Load data from the resource file:
public Builder setMultiChoiceItems(int itemsId, boolean[]checkedItems, final OnMultiChoiceListener listener)
2. Load data from the dataset
public Builder setMultiChoiceItems(Cursor cursor, StringisCheckedColumn, String labelColumn, final OnMultiChoiceListenerlistener)
3. Load data from a string array
public Builder setMultiChoiceItems(CharSequence[] items,boolean[] checkedItems, final OnMultiChoiceListener listener)
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class DiaActivity extends Activity { /** Called when the activity is first created. */ private Button btn; private TextView tv; private String[] shuxing = { "Font Color", "Font Size" }; private boolean[] chuzhi = { false, false }; @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); btn = (Button) findViewById(.button1); tv = (TextView) findViewById(.textView1); (new () { @Override public void onClick(View v) { // TODO Auto-generated method stub Builder b = new (); ("gggggg"); (shuxing, chuzhi, new () { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { switch (which) { case 0: chuzhi[0] = isChecked; break; case 1: chuzhi[1] = isChecked; break; default: break; } } }); ("Sure", new () { @Override public void onClick(DialogInterface dialog, int which) { if (chuzhi[0] == true) { (); } if (chuzhi[1] == true) { (50f); } } }); ().show(); } }); } }
2. The setSingleChoiceItems method of the class can create a list with a single button.: The method is as follows:
1. Load data from resource file:
public Builder setSingleChoiceItems(int itemsId, intcheckedItem, final OnClickListener listener)
2. Load data from the dataset
public Builder setSingleChoiceItems(Cursor cursor, intcheckedItem, String labelColumn, final OnClickListenerlistener)
3. Load data from a string array
public Builder setSingleChoiceItems(CharSequence[] items, intcheckedItem, final OnClickListener listener)
4. Load data from ListAdapter object
public Builder setSingleChoiceItems(ListAdapter adpater, intcheckedItem, final OnClickListener listener)
import ; import ; import ; import ; import ; import ; import ; import ; import ; public class SingleChoiceItemsTest extends Activityimplements OnClickListener { private String[] province = new String[] { "Shanghai", "Beijing", "Hunan", "Hubei", "Hainan" }; private Button btnSingleChoiceList; // Click the instance of the event object private ButtonOnClick buttonOnClick = new ButtonOnClick(1); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); btnSingleChoiceList = (Button) findViewById(); (this); } @Override public void onClick(View v) { showSingleChoiceButton(); } // Show OK and Cancel buttons in the radio option //The data type of the buttonOnClick variable is ButtonOnClick, a click event class private void showSingleChoiceButton() { builder = new (this); ("Please select a province"); (province, 0, buttonOnClick); ("Sure", buttonOnClick); ("Cancel", buttonOnClick); (); } private class ButtonOnClick implements { private int index; // Indicates the index of the options public ButtonOnClick(int index) { = index; } @Override public void onClick(DialogInterface dialog,int which) { // which represents the button index of the clicked. All option indexes are greater than 0 and button indexes are less than 0. if (which >= 0) { //If you click on a list item, save the index of the current list item in the index. //If you want to close the dialog box after clicking the list item, you can call () here // Or use the () method. index = which; } else { //The user clicked the [OK] button if (which == DialogInterface.BUTTON_POSITIVE) { //Show the list item selected by the user. final AlertDialog ad = new ( ).setMessage( "Your choice is:" + index + ":" + province[index]).show(); //It will automatically close after five seconds. Handler hander = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { (); } }; (runnable, 5 * 1000); } //The user clicked the [Cancel] button else if (which == DialogInterface.BUTTON_NEGATIVE) { (, "You didn't choose anything", Toast.LENGTH_LONG); } } } } }
For more information about Android related content, please check out the topic of this site:Summary of Android graphics and image processing skills》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Android multimedia operation skills summary (audio, video, recording, etc.)》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.