2

I wrote some code which implements the listView inside alertDialog. now when i want to implement some function on the listview I didnt succeed doing it. here is the code to he alertDialog:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Name");
ListView modeList = new ListView(this);
String[] stringArray = new String[] {"red" , "green" , "blue"};
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this,         android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
modeList.setAdapter(modeAdapter);
builder.setView(modeList);
final Dialog dialog = builder.create();
dialog.show();

how can i implement a function on a clicked item?

donfuxx
  • 11,277
  • 6
  • 44
  • 76
Jimmi.Dvelo
  • 51
  • 1
  • 6

2 Answers2

0

You can try build in implementation of list in alert dialog. It depends on use case. If you want to use this dialog just for picking value from the list (without custom list item UI) - it is good approach.

Community
  • 1
  • 1
x90
  • 2,140
  • 15
  • 25
0

You should be able to do something like this code : it shows an alertDialog with a listView containing your values :

CharSequence[]  stringArray =  {"red" , "green" , "blue"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Name");
builder.setItems(stringArray, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
         // Do something with the selection
    }
});
AlertDialog alert = builder.create();
alert.show();
Zied R.
  • 4,964
  • 2
  • 36
  • 67