是否可以在警报对话框完成其工作时停止进程?

Is possible to stop process while alert dialog finish its work?

这是我的方法

public void getValues() {
            alertDialog = new AlertDialog.Builder(MainActivity.this);
            LayoutInflater inflater = this.getLayoutInflater();
            alertDialog.setView(inflater.inflate(R.layout.custom_layout, null));

            alertDialog.setCancelable(false);

            alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    field1 = (EditText)((Dialog) dialog).findViewById(R.id.field1);
                    field2 = (EditText)((Dialog) dialog).findViewById(R.id.field2);
                    errorMsg = (TextView)((Dialog) dialog).findViewById(R.id.login_error);
                    insert();
                }
            });

            /* When negative (No/cancel) button is clicked*/
            alertDialog.setNegativeButton("Back", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel(); // Your custom code
                }
            });
            alertDialog.show();
}

我需要此方法来创建一个输入对话框,以获取我需要在我的数据库中检查的值。

这些方法适用于此代码

MyClass object = createObject();
while(!checkDB(object))
        {
            //Alert Dialog
            getValues();
        }

好吧,createObject() 在另一部分代码中获取一些信息并从 class MyClass 创建一个对象。 checkDB 在我的数据库中搜索对象;

我的逻辑是:如果我搜索的对象不存在,我将使用 AlertDialog(方法 getValues() 创建它,使用方法 insert() 在数据库中插入新对象)。

问题是,如果我使用 while 过程,我的 AlertDialog 将不会创建,所以 !checkDB(object) 总是 false!

我怎样才能暂时使用这个alertDialog?

感谢回答

编辑:

我可以使用其他 2 个选项:

MyClass object = createObject()
if(checkDB(object)
{
     continueOtherMethods();
}
else
{
     getValues();
}

MyClass object = createObject()
    if(!checkDB(object)
    {
         getValues();
    }
    continueOtherMethods();

第二个选择是我最初的选择。问题是 continueOtherMethods() 在我从 AlertDialog 获取数据之前工作。

第一个选择的问题是,当应用程序正确执行 getValues() 时,没有执行 continueOtherMethods()。

你的while循环实际上是主线程上的无限循环。这就是永远不会显示警报对话框的原因。

How can I use this alertDialog in the while?

你不应该拖延主线程。将 while 语句替换为 if 语句,并在对话框完成其工作后继续执行代码:

public void getValues() {
        alertDialog = new AlertDialog.Builder(MainActivity.this);
        LayoutInflater inflater = this.getLayoutInflater();
        alertDialog.setView(inflater.inflate(R.layout.custom_layout, null));

        alertDialog.setCancelable(false);

        alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                field1 = (EditText)((Dialog) dialog).findViewById(R.id.field1);
                field2 = (EditText)((Dialog) dialog).findViewById(R.id.field2);
                errorMsg = (TextView)((Dialog) dialog).findViewById(R.id.login_error);
                insert();
                continueWhereLeftOff();
            }
        });

        /* When negative (No/cancel) button is clicked*/
        alertDialog.setNegativeButton("Back", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel(); // Your custom code
                continueWhereLeftOff();
            }
        });
        alertDialog.show();
}


MyClass object = createObject();
if(!checkDB(object)){
    getValues();//Alert Dialog
}
else{
    continueWhereLeftOff();
}
//remove all code below this line to the continueWhereLeftOff()