AlertDialog with an ArrayAdapter not displaying a list of items

I want to create an AlertDialog that displays a list of a custom Supplier object. The toString() method has been overridden to display a description.

 AlertDialog.Builder dialog = new AlertDialog.Builder(ExampleActivity.this); dialog.setTitle("Title"); dialog.setMessage("Message:"); final ArrayAdapter<Supplier> adapter = new ArrayAdapter<Supplier>( ExampleActivity.this, android.R.layout.select_dialog_singlechoice); adapter.add(new Supplier()); adapter.add(new Supplier()); adapter.add(new Supplier()); dialog.setAdapter(adapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); dialog.setNegativeButton("Cancel", null); dialog.show(); 

From the examples I looked at, I cannot find anything obviously wrong with this code. However, when I run it, it does not display the list of provider objects as expected. I also tried using the setItems and setSingleChoiceItems for AlertDialog.Builder . Can anyone see where I'm wrong?

+4
source share
2 answers

It turns out you cannot install the message and adapter together. It works when I delete dialog.setMessage("Message:") .

+8
source

you can use this:

 AlertDialog.Builder builderSingle = new AlertDialog.Builder(context); builderSingle.setIcon(R.drawable.logobig); builderSingle.setTitle(""); ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>( context, android.R.layout.simple_list_item_1 ); arrayAdapter.add("Customer 1 "); arrayAdapter.add("Customer 2"); builderSingle.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: // handle item1 break; case 1: // item2 break; case 2: // item3 break; default: break; } } }); builderSingle.show(); 
+1
source

Source: https://habr.com/ru/post/1494323/


All Articles