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?
Turns out you can't set a message and an adapter together. It works when I remove dialog.setMessage("Message:")
.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With