I'm creating an AlertDialog
with setting array (R.array.select_dialog_items
) of items to it:
<string-array name="select_dialog_items">
<item>Start</item>
<item>Stop</item>
<item>Delete</item>
</string-array>
I don't know how to switch among these items in onClick()
method, can you advice?
public void init() {
builder = new AlertDialog.Builder(mContext);
builder.setTitle("Task");
builder.setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dial开发者_C百科og, int item) {
switch (item){
case : // How to iterate here?
}
}
});
alert = builder.create();
}
int item
of the onClick
method represents the position of the item clicked in the alertDialog (as mentioned in the API here. Therefore:
public void init() {
builder = new AlertDialog.Builder(mContext);
builder.setTitle("Task");
builder.setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch (item){
case 0: // Start item
some code...
break;
case 1: // Stop item
some code...
break;
case 2: // Delete item
some code...
break;
}
}
});
alert = builder.create(); }
Give that a try.
精彩评论