android – 如何在onPrepareDialog中设置setSingleChoiceItems的内容?

前端之家收集整理的这篇文章主要介绍了android – 如何在onPrepareDialog中设置setSingleChoiceItems的内容?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
伙计们,在onCreateDialog上我有这个:
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,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();
    }

这很有效.

但要注意线

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

假设每次弹出对话框时都会更改phoneNums.
我已经覆盖onPrepareDialog方法,但我不知道如何为它分配新值.
并且那里没有setSingleChoiceItems.

这是我的onPrepareDialog方法

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;
}

解决办法是什么?
先谢谢你们.

解决方法

您需要使用AlertDialog类中的 getListView方法.然后在返回的对象上使用 setItemChecked方法.例:

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

猜你在找的Android相关文章