How to set setSingleChoiceItems content in onPrepareDialog?

Guys, in onCreateDialog I have this:

case DIALOG_REVIEW: { if (bundle.containsKey("POSITION")) { final int position = bundle.getInt("POSITION"); ArrayList<String> alterNumbers = numbers.get(position); final String[] phoneNums = new String[alterNumbers.size()]; for (int i = 0; i < alterNumbers.size(); i++) { phoneNums[i] = alterNumbers.get(i); } AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle(names.get(position) + " number(s)"); dialog.setSingleChoiceItems(phoneNums, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // get selected item and close the dialog String selectedNumber = phoneNums[which]; updateUserSelectedNumber(position , selectedNumber); } }); return dialog.create(); } 

which works and is excellent.

BUT pay attention to the line

 dialog.setSingleChoiceItems(phoneNums, 0, new DialogInterface.OnClickListener() { 

phoneNums should change every time a dialog appears. I override the onPrepareDialog method, but I do not know how to assign new values ​​to it. and also no setSingleChoiceItems.

here is my onPrepareDialog method

 case DIALOG_REVIEW: { final int position = bundle.getInt("POSITION"); ArrayList<String> alterNumbers = numbers.get(position); final String[] phoneNums = new String[alterNumbers.size()]; for (int i = 0; i < alterNumbers.size(); i++) { phoneNums[i] = alterNumbers.get(i); } AlertDialog alertDialog = (AlertDialog) dialog; alertDialog.setTitle(names.get(position) + " number(s)"); ??? break; } 

What's the solution? thanks in advance guys.

+7
source share
2 answers

You need to use the getListView method from the AlertDialog class. Then use the setItemChecked method for the returned object. Example:

alertDialog.getListView().setItemChecked(1, true);

+10
source

I ran into the same problem:

Two solutions:

1 / Fast and dirty

Delete the dialog every time you are done with it => onPrepareDialog will not be called, so you will not have problems updating the data:

 protected Dialog onCreateDialog(int id) { ... case DIALOG_REVIEW: { AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle(names.get(position) + " number(s)"); dialog.setSingleChoiceItems(phoneNums, 0,new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog,int which) { // get selected item and close the dialog String selectedNumber = phoneNums[which]; updateUserSelectedNumber(position , selectedNumber); removeDialog(DIALOG_REVIEW); } }); return dialog.create(); } 

if you prefer, you can put onDismissListener and remove removeDialog in the dialog box.


2 / Pretty pretty

In the onPrepareDialog method, simply replace the old ArrayAdapter used by the dialog with a new fresh one:

 @Override protected void onPrepareDialog(int id, Dialog dialog) { switch (id) { case DIALOG_REVIEW: ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.select_dialog_singlechoice, android.R.id.text1, phoneNums); AlertDialog ad = (AlertDialog) dialog; ad.getListView().setAdapter(adapter); break; default: super.onPrepareDialog(id, dialog); } } 

I use the same method as the one used by android (AlertController.java L. 854 froyo source code) to populate the dialog for the first time.

+7
source

All Articles