Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ArrayAdapter with AlertDialog and .setAdapter

My code goes inside an OnOptionsItemSelected method. I've tried displaying a simple toast and it works fine, so at least I know I'm "getting there".

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_multichoice);
adapter.add("whatever data1");
adapter.add("whatever data2");
adapter.add("whatever data3");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("whatever title");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {

    }
});

The problem is that there's no alert dialog. I've tried constructing an alert dialog with simple arrays, which works.

like image 861
HSPalm Avatar asked Feb 06 '12 18:02

HSPalm


People also ask

What is the use of Arrayadapter?

You can use this adapter to provide views for an AdapterView , Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner .

What is the significance of AlertDialog builder?

Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue.

How many buttons can be set up on an AlertDialog?

AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

What is the difference between dialog and AlertDialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .


1 Answers

I think you are missing the

AlertDialog alert = builder.create();
alert.show();
like image 110
Heiko Rupp Avatar answered Sep 20 '22 18:09

Heiko Rupp