选择项目后,Android Listview对话框不会消失

时间:2013-04-04 04:13:52

标签: android listview dialog

我尝试创建列表视图对话框以显示选择列表。我的代码如下所示:

        LayoutInflater factory=LayoutInflater.from(this);
    final View stuckLevelDialogView=factory.inflate(R.layout.report_stuck_dialog, null);

    final ListView stuckLevelListViewForDialog=(ListView)stuckLevelDialogView.findViewById(R.id.report_stuck_dialog_listview);
    final String[] stuckLevelList=new String[]{"1 - You can move freely","2 - You have to be aware of your movement","3 - You can move slowly","4 - There is a traffic jam","5 - There is a serious traffic jam"};
    ArrayAdapter<String> adapterForDialog=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stuckLevelList);
    stuckLevelListViewForDialog.setAdapter(adapterForDialog);

    final AlertDialog.Builder stuckLevelDialog=new AlertDialog.Builder(this);
    stuckLevelDialog.setTitle("What stuck level is this point?");
    stuckLevelDialog.setView(stuckLevelDialogView);
    stuckLevelDialog.show();

但是,当我选择一个选项时,会执行onItemClick,但listview对话框不会消失,我必须手动按下后退按钮。我试着整整一天调试代码,但还没有解决。请帮我。先谢谢!

3 个答案:

答案 0 :(得分:0)

我认为您需要在dismiss()听众中onItemClick对话,如下所示:

    stuckLevelListViewForDialog.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> p_arg0, View p_arg1,
                int p_arg2, long p_arg3) {
        stuckLevelDialog.dismiss();
        }
    });

答案 1 :(得分:0)

使用stuckLevelDialog.dismiss;在onItemClick结束时。

答案 2 :(得分:0)

您可以在警报对话框中使用项目列表设置setSingleChoiceItems,该列表将显示带有单选按钮的列表。如果您想添加按钮,则可以在用户选择任何项目时dismiss dialog

new AlertDialog.Builder(this)
    .setSingleChoiceItems(array, -1, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // here you can do your functionality and can dismiss dialog as well
                    dialog.dismiss();

                }
            })        
    .show();