单击“列表”项时隐藏AlertDialog

时间:2017-09-19 12:05:03

标签: android android-alertdialog

我创建了一个包含TextView的警告对话框和一个包含选项的列表。用户可以从列表中选择一个项目,也可以使用EditText添加新项目。

问题是alertDialog仅在我按下按钮添加新内容时隐藏,而不是在我点击列表中的项目时隐藏。

final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    final LayoutInflater inflater = getLayoutInflater();
    final View dialogGroup = inflater.inflate(R.layout.select_group_layout, null);
    final EditText newGroup = dialogGroup.findViewById(R.id.new_group);
    final ListView listView = dialogGroup.findViewById(R.id.groups_list);


    listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list));
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            HelperTools.showInfo("Selected group: " + ((TextView) view).getText(), getClass());
        }
    });

    builder.setView(dialogGroup)
            .setTitle(R.string.departments_txt)
            .setPositiveButton("Añadir", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    contactDepartment = newGroup.getText().toString();
                    HelperTools.showInfo("Selected group: " + contactDepartment, getClass());
                }
            });

    if (list.size() != 0) {
        builder.create();
        builder.show();
    }

如何在选择ListView项目时隐藏AlertDialog?

感谢。

修改

最终代码:

    final AlertDialog builder = new AlertDialog.Builder(this).create();
    final LayoutInflater inflater = getLayoutInflater();
    final View dialogGroup = inflater.inflate(R.layout.select_group_layout, null);
    final EditText newGroup = dialogGroup.findViewById(R.id.new_group);
    final ListView listView = dialogGroup.findViewById(R.id.groups_list);


    listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list));
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            HelperTools.showInfo("Selected group: " + ((TextView) view).getText(), getClass());
            builder.dismiss();
        }
    });

    builder.setView(dialogGroup);
    builder.setTitle(R.string.departments_txt);
    builder.setButton(Dialog.BUTTON_POSITIVE, "Añadir", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            contactDepartment = newGroup.getText().toString();
            HelperTools.showInfo("Selected group: " + contactDepartment, getClass());
        }
    });

    if (list.size() != 0) {
        builder.show();
    }

3 个答案:

答案 0 :(得分:2)

添加builder.dismiss();点击列表视图中的项目后。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
        HelperTools.showInfo("Selected group: " + ((TextView) view).getText(), getClass());
        builder.dismiss();
    }
});

修改

像这样创建AlterDialog

final AlertDialog builder = new AlertDialog.Builder(this).create();

显示对话框

builder.show();

取消对话

builder.dismiss();

答案 1 :(得分:0)

您可以使用 Dialog.dismiss()

  

<强> dismiss()   关闭此对话框,将其从屏幕上删除。

 AlertDialog dialog=builder.create();
                            //then put dialog.dismiss() to 
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
        HelperTools.showInfo("Selected group: " + ((TextView) view).getText(), getClass());
        if (dialog.isShowing()){
            dialog.dismiss();
        }

    }
});

答案 2 :(得分:0)

而不是此代码:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
        HelperTools.showInfo("Selected group: " + ((TextView) view).getText(), getClass());


    }
});

使用此代码:

builder.setSingleChoiceItems(list, position, new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int position)
            {
                // do something
                dialog.dismiss();
            }
        });