【问题标题】:How can inflate a layout containing listview in a alert dialog?如何在警报对话框中膨胀包含列表视图的布局?
【发布时间】:2011-10-14 05:38:24
【问题描述】:

我在布局中使用带有自定义适配器的列表视图。 现在我正在尝试将包含列表的布局带到我的警报对话框中。 我尝试使用此代码将不包含列表的简单布局引入警报对话框,并且运行良好。 但我无法将包含布局的列表带入 alertdialog。

           AlertDialog.Builder dialog = new AlertDialog.Builder( this );
           dialog.setView( getLayoutInflater().inflate( R.layout.smill, null ) );
           dialog.setIcon(R.drawable.androidsmile);
           dialog.setInverseBackgroundForced(true);


           dialog.setTitle( "Select smiley");
           dialog.setPositiveButton( "Cancel", null );
           dialog.show();  

【问题讨论】:

  • 您能详细说明您的应用程序出了什么问题吗?它是否强制关闭或列表视图未显示或其他任何内容。

标签: android listview android-alertdialog custom-adapter


【解决方案1】:

您所做的只是将视图膨胀到警报对话框中。您没有在该列表视图上设置适配器,所以它当然看起来不起作用(因为它是空的)。

您需要执行以下操作:

View view = getLayoutInflater().inflate( R.layout.smill, null);
ListView listView = (ListView) view.findViewById(R.id.listView);
YourCustomAdapter adapter = new YourCustomAdapter(parameters...);
listView.setAdapter(adapter);

AlertDialog.Builder dialog = new AlertDialog.Builder( this );
dialog.setView(view);
...
...
...
dialog.show();  

【讨论】:

  • 避免将 null 作为 layoutInflater 的根视图。如果这是在您的适配器内部完成的,您可以传递 rootview 或 convertview 或任何托管此警报对话框的视图
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-28
相关资源
最近更新 更多