How can inflate a layout containing listview in a alert dialog?

倾然丶 夕夏残阳落幕 提交于 2019-12-09 14:45:56

问题


I am using a listview with a custom adapter in a layout. Now i am trying to bring that layout containing list to my alertdialog. I tried to bring simple layouts not containing list to alert dialog with this code and its is working good. But i am unable to bring the list containing layout into 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();  

回答1:


All you are doing is inflating a view into your alert dialog. You aren't setting the adapter on that listview so of course it's going to appear to not be working (since its empty).

You need to do something like:

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();  


来源:https://stackoverflow.com/questions/7763506/how-can-inflate-a-layout-containing-listview-in-a-alert-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!