【问题标题】:how to set contents of setSingleChoiceItems in onPrepareDialog?如何在 onPrepareDialog 中设置 setSingleChoiceItems 的内容?
【发布时间】:2023-04-03 12:19:01
【问题描述】:

伙计们,在 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, 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();
    }

这很好用。

但要注意行

dialog.setSingleChoiceItems(phoneNums, 0,
        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;
}

解决办法是什么? 在此先感谢各位。

【问题讨论】:

    标签: android dialog android-activity android-alertdialog


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      我刚遇到同样的问题:

      两种解决方案:

      1/ 又快又脏

      每次完成后删除对话框 => onPrepareDialog 不会被调用,因此您不会遇到数据更新问题:

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

      如果您愿意,可以输入 onDismissListener 并在对话框关闭时执行 removeDialog。


      2/ 相当漂亮的一个

      在 onPrepareDialog 方法中,只需将对话框使用的旧 ArrayAdapter 替换为新的:

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

      我第一次使用与 android 相同的方法(froyo 源代码的 AlertController.java L. 854)来填充对话框。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多