我想要创建一个AlertDialog
,它将显示自定义对象Supplier
的列表。toString()
方法已被重写以显示描述。
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();
从我看过的例子中,我找不到这段代码的任何明显错误。但是,当我运行它时,它并没有像预期的那样显示供应商对象的列表。我还尝试为setItems
和setSingleChoiceItems
方法使用AlertDialog.Builder
。有人能看出我哪里出了问题吗?
发布于 2013-07-30 15:42:08
原来你不能把一个消息和一个适配器放在一起。当我移除dialog.setMessage("Message:")
时,它可以工作。
发布于 2014-02-23 07:16:46
你可以用这个:
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();
https://stackoverflow.com/questions/17950319
复制相似问题