Before 2022/Android

List to AlertDialog

Eljoe 2018. 12. 4. 17:55
final List ListItems = new ArrayList<>();
ListItems.add("사과");
ListItems.add("배");
ListItems.add("귤");
ListItems.add("바나나");

final CharSequence[] items =  ListItems.toArray(new String[ ListItems.size()]);
 
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("AlertDialog Title");
       .setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int pos) {
                String selectedText = items[pos].toString();
                Toast.makeText(MainActivity.this, selectedText, Toast.LENGTH_SHORT).show();
            }
        }).creat().show();