how to set contents of setSingleChoiceItems in onPrepareDialog?

匿名 (未验证) 提交于 2019-12-03 02:52:02

问题:

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) + "'s 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 is working and great.

BUT pay attention to line

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

phoneNums are suppose to be changing each time the dialog pops up. I've overriden onPrepareDialog method but I don't know how to assign new values to it. and also there is no setSingleChoiceItems there.

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) + "'s number(s)");     ???     break; }

What is the solution? thanks in advance guys.

回答1:

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

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



回答2:

I just faced the same problem:

Two solutions:

1/ The fast and dirty

Delete the Dialog each time you have finished with it => onPrepareDialog will not be called so you wont have problems with data update:

protected Dialog onCreateDialog(int id) {     ...     case DIALOG_REVIEW: {         AlertDialog.Builder dialog = new AlertDialog.Builder(this);         dialog.setTitle(names.get(position) + "'s 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 a onDismissListener and do the removeDialog on the dialog dismiss.


2/ The quite pretty one

In the onPrepareDialog method just replace the old ArrayAdapter used by the dialog by a fresh new 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 than the one used by android (the AlertController.java L. 854 of the froyo source code) to populate the dialog the first time.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!