简体   繁体   中英

Cannot resolve method setItems android alert dialog

I am working on how to add an arraylist to an alert dialog. When I call the alertdialog.setItems I am getting a cannot resolve this method error. Could anybody take a look and lead me towards how to fix this? Thanks in advance.

The code:

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

I see the problem. In the Android docs, you are creating an AlertDialog and defining it as an AlertDialog.Builder, but the setItems() method is only valid on an AlertDialog.Builder. I would define 'ad' as an AlertDialog.Builder, then create an AlertDialog from the builder and show it like this:

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

Read the docs for more info: AlertDialog.Builder , AlertDialog

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM