我正在尝试使用Android应用上的列表构建AlertDialog 这是我用来构建对话框的代码:
AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
builder.setTitle("Pick a branch to navigate to:");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1);
adapter.addAll(branches);
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent locationIntent = new Intent(getApplicationContext(), NavigationActivity.class);
locationIntent.putExtra("branch", which);
startActivity(locationIntent);
}
});
builder.show();
只是为了说清楚:branches
是Set<String>
。
在有意活动的日志中,我收到一条消息:
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
我得到上面的例外,因为命令使用转换的Set<String>
到String[]
,这就是为什么AlertDialog为空。
我如何解决它并显示一个带有列表的AlertDialog包含来自Set<String>
的项目?
答案 0 :(得分:0)
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Schedule Post");
String[] items={"Item 1","Item 2","Item 3"};
alertDialogBuilder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();