I have the next code, I’m trying to return the value of “players” in this method, based on the user selection. The problem I can do this with onClick method of the dialog, any idea?
public int dialogoInicial(){
int players = 1;
final String[] playersArray = {"1","2","3","4","5","6"};
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Nº "+getString(R.string.players)+":");
dialog.setSingleChoiceItems(playersArray, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int player) {
players = Integer.parseInt(playersArray[player]);
}
});
dialog.show();
return players;
}
This problem here is that the
return playerswill be executed while the dialog is being shown. The simple solution is to make theplayersvariable a class level variable instead of a local variable to the function. You can also make your function returnvoidsince it won’t be able to return anything. Android Dialogs areAsynchronous.