带有列表的 AlertDialog 显示空对话框
AlertDialog with a list shows empty dialog
在我的 Android 应用程序中,我有一个应该显示列表的对话框:
final 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 searchAddress = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + branches.toArray()[which]));
startActivity(searchAddress);
}
});
builder.show();
当我启动这段代码时,我会看到一个带有标题的对话框,下面是空白 space,但是当我单击空白 space 时,它会执行操作并使用正确的内容,所以视图有问题。
我能用它做什么?
如果您的 onClick 操作正常,则意味着列表正确填充只是显示视图有问题。因此,请检查您的适配器 class。
编辑:
将应用程序上下文更改为 Activity 的上下文
在我的 Android 应用程序中,我有一个应该显示列表的对话框:
final 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 searchAddress = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + branches.toArray()[which]));
startActivity(searchAddress);
}
});
builder.show();
当我启动这段代码时,我会看到一个带有标题的对话框,下面是空白 space,但是当我单击空白 space 时,它会执行操作并使用正确的内容,所以视图有问题。
我能用它做什么?
如果您的 onClick 操作正常,则意味着列表正确填充只是显示视图有问题。因此,请检查您的适配器 class。
编辑: 将应用程序上下文更改为 Activity 的上下文