【问题标题】:Cannot resolve method setItems android alert dialog无法解析方法 setItems android 警报对话框
【发布时间】:2014-10-13 20:00:40
【问题描述】:

我正在研究如何将数组列表添加到警报对话框。当我调用alertdialog.setItems 时,我收到了无法解决此方法的错误。有人可以看看并引导我解决这个问题吗?提前致谢。

代码:

if(arrayListBluetoothDevices.size()<1) // this checks if the size of bluetooth device is 0,then add the
{                                           // device to the arraylist.
    detectedAdapter.add(device.getName()+"\n"+device.getAddress());
    arrayListBluetoothDevices.add(device);
    final CharSequence[] items2 = {"This is here just to figure out how to get setItems to call properly"};

    AlertDialog ad = new AlertDialog.Builder(context).create();
    ad.setTitle("Pop up the found devices here");
    ad.setItems(items2, null);
    ad.setButton("Somehow set this to work when the arraylist is pressed", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            //Do stuff here for OK The bottom button
        }
    });
    ad.show();
    detectedAdapter.notifyDataSetChanged();
}

【问题讨论】:

  • 请发布堆栈跟踪
  • 这是我得到的编译错误:错误:(332, 23) 错误: 找不到符号方法 setItems(CharSequence[],)。这就是你要找的东西吗,我无法进入应用程序进行堆栈跟踪。

标签: java android arraylist android-arrayadapter android-alertdialog


【解决方案1】:

我看到了问题。在 Android 文档中,您正在创建 AlertDialog 并将其定义为 AlertDialog.Builder,但 setItems() 方法仅在 AlertDialog.Builder 上有效。我会将“广告”定义为 AlertDialog.Builder,然后从构建器创建一个 AlertDialog 并显示如下:

if(arrayListBluetoothDevices.size()<1) // this checks if the size of bluetooth device is 0,then add the
            {                                           // device to the arraylist.
                detectedAdapter.add(device.getName()+"\n"+device.getAddress());
                arrayListBluetoothDevices.add(device);
                final CharSequence[] items2 = {"This is here just to figure out how to get setItems to call properly"};

                AlertDialog.Builder adb = new AlertDialog.Builder(context).create();
                adb.setTitle("Pop up the found devices here");
                adb.setItems(items2, null);
                AlertDialog ad = adb.create();
                ad.setButton("Somehow set this to work when the arraylist is pressed", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        //Do stuff here for OK The bottom button
                    }
                });
                ad.show();
                detectedAdapter.notifyDataSetChanged();
            }

阅读文档了解更多信息:AlertDialog.BuilderAlertDialog

【讨论】:

    猜你喜欢
    • 2015-04-08
    • 2013-09-23
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    相关资源
    最近更新 更多