Android:重用AlertBuilder实例

时间:2019-07-08 04:58:36

标签: android alertdialog

在使用此对话框之前,我已经创建了一个方法,并且只调用了一次此方法:

 private void makeAlertDialogBuilder() {
        binding = DataBindingUtil.inflate(LayoutInflater.from(getContext()),
                R.layout.revisited_dialog, null, false);
        alertBuilder = new AlertDialog.Builder(getContext()).setView(binding.getRoot())
                .setView(binding.getRoot());
        binding.btnCancel.setOnClickListener(v -> alertDialog.dismiss());
    }

我在工具栏中有一个菜单,所以我想单击添加菜单项,此对话框打开并显示新消息,当我单击编辑菜单项时显示具有新消息的对话框:

toolbar.setOnMenuItemClickListener(item -> {
    int id = item.getItemId();
    if (id == R.id.add_row) {
        alertDialog = alertBuilder
                .setMessage("Add")
                .setCancelable(false)
                .show();

        exGridAdapter = new RevisitGridAdapter(gridAdapter, baseClass, Action.ADD);
        initRecyclerView(exGridAdapter);
        return true;

    } else if (id == R.id.edit_row) {
        if (selectedItem != null) {
            alertDialog = alertBuilder
                    .setMessage("Edit")
                    .setCancelable(false)
                    .show();
            exGridAdapter = new RevisitGridAdapter(gridAdapter, baseClass, Action.EDIT, selectedItem);
            initRecyclerView(exGridAdapter);
        } 

对于第一次单击,显示对话框,但是当它被取消并再次单击时,我得到了ANR错误:

  

指定的孩子已经有一个父母。您必须先在孩子的父母上调用removeView()。

我从alertBuilder创建了一个对象,我想使用它吗?有可能还是我必须重新创建新的alertBuilder对象?

4 个答案:

答案 0 :(得分:0)

AlertDialog.Builder alertDialog;  // Your Global Object

    AlertDialog.Builder getAlertDialogObject() {
        if (alertDialog != null) {
            return alertDialog;
        } else {
            alertDialog = new AlertDialog.Builder(LoginIssueScreen.this);
        }
        return alertDialog;
    }

您需要为AlertDialog创建一个通用方法:-

    public void showAlerDialogResult(String msg, String title) {

            AlertDialog.Builder alertDialog = getAlertDialogObject();
            alertDialog.setMessage(msg);
            alertDialog.setIcon(R.drawable.ic_check_green);
            alertDialog.setTitle(title);
            alertDialog.setCancelable(false);
            alertDialog.setPositiveButton("OK", (dialogInterface, i) -> {
                // Your code on click on ok

            });
            alertDialog.setNegativeButton("Cancel", (dialogInterface, i) -> {
             // Your code on click on Cancel

        });
            alertDialog.show();

        }

并按如下所示进行命名-:

showAlerDialogResult("Add Button clicked", "Click Event");

                or

showAlerDialogResult("Edit Button clicked", "Click Event");

答案 1 :(得分:0)

您可以在单独的类中使用类似的内容,例如,我使用过AlertUtils.java:

 public class AlertUtils
{
   public static void showOKDialog(Context context, String title, String message)
    {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.setPositiveButton(android.R.string.ok, null);
    builder.show();
    }
}

在此方法中,您通过的上下文可以是您的Activity的this,例如:MyActivity.this或片段的getContext()

AlertUtils.showOKDialog(MyActivity.this,“对话框标题”,“在对话框中显示的消息”);

答案 2 :(得分:0)

发生此问题是由于再次打开同一对话框而没有关闭上一个对话框。

您需要检查第一个对话框是否打开,然后将其关闭。喜欢,

toolbar.setOnMenuItemClickListener(item -> {
    int id = item.getItemId();
    if (id == R.id.add_row) {
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
        }
        alertDialog = alertBuilder
                .setMessage("Add")
                .setCancelable(false)
                .show();

        exGridAdapter = new RevisitGridAdapter(gridAdapter, baseClass, Action.ADD);
        initRecyclerView(exGridAdapter);
        return true;

    } else if (id == R.id.edit_row) {
        if (selectedItem != null) {
            if (alertDialog != null && alertDialog.isShowing()) {
                alertDialog.dismiss();
            }
            alertDialog = alertBuilder
                    .setMessage("Edit")
                    .setCancelable(false)
                    .show();
            exGridAdapter = new RevisitGridAdapter(gridAdapter, baseClass, Action.EDIT, selectedItem);
            initRecyclerView(exGridAdapter);
        } 

答案 3 :(得分:0)

我终于通过从父视图中删除视图并使用此方法再次附加视图来解决了我的问题:

open()

我在问题中提到了public void showAlerDialogResult(String title) { ViewGroup parent = (ViewGroup) binding.getRoot().getParent(); if (parent != null) { parent.removeView(binding.getRoot()); } try { makeAlertDialogBuilder(); } catch (InflateException e) { Logger.log(e.getMessage()); } AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getContext()) .setView(binding.getRoot()); alertDialog = alertBuilder .setTitle(title) .setCancelable(false) .show(); } 方法。 希望对您有所帮助。谢谢大家

坦克到makeAlertDialogBuilder();