4

我想创建一个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();

从我查看的示例中,我找不到此代码有任何明显错误的地方。但是,当我运行它时,它没有按预期显示供应商对象的列表。我也尝试setItemssetSingleChoiceItemsAlertDialog.Builder. 谁能看到我哪里出错了?

4

2 回答 2

9

事实证明,您不能同时设置消息和适配器。当我删除它时它工作dialog.setMessage("Message:")

于 2013-07-30T15:42:08.943 回答
1

你可以使用这个:

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();
于 2014-02-23T07:16:46.073 回答