4

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?

2 Answers 2

10

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

Sign up to request clarification or add additional context in comments.

3 Comments

Any idea why they can't go together?
I think it was a design choice more than anything. They are mutually exclusive so you can only use one or the other. A way around it is to create a layout with a TextView and a ListView, inflate it, do what you want to do with it and use the setView method. The problem I found with that method though is that the ListView will sometimes appear off screen if the list is large. So you may need to give it a fixed height. Hope that helps!
Wow, I just spent the last hour screaming at my computer over this.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.