Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlertDialog with ArrayAdapter not showing list of items

I want to create an AlertDialog that will display a list of a custom object Supplier. 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 have looked at, I can't find anything obviously wrong with this code. However, when I run it, it doesn't show a list of the supplier objects as expected. I've also tried using the setItems and setSingleChoiceItems methods for the AlertDialog.Builder. Can anyone see where I am going wrong?

like image 908
Stuart Robertson Avatar asked Dec 26 '22 23:12

Stuart Robertson


2 Answers

Turns out you can't set a message and an adapter together. It works when I remove dialog.setMessage("Message:").

like image 96
Stuart Robertson Avatar answered Jan 11 '23 10:01

Stuart Robertson


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();
like image 26
user3266062 Avatar answered Jan 11 '23 10:01

user3266062