Android 警告框不会保持打开状态

Android Alert Box Wont Stay Open

正如标题所说,我的应用程序中的警告框不会保持打开状态。该框嵌套在单击按钮时选中的 for 循环中。当循环进入警告框时,它只是在屏幕上闪烁,然后立即消失。

我可以在警告框上放置某种计时器以使其保持打开状态,还是我错误地实现了该框?

带 for 循环和嵌套警告框的按钮

Button button= (Button) findViewById(R.id.cookButton);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
            //deducting one ingredient
            final Cursor ingredients = adapter.getRecipesIngredients(recipeCode);
            final Cursor missing = adapter.missingIngredients(recipeCode);
            if(missing.getCount() == 0)
            {
                Toast.makeText(getApplicationContext(), "Haveeverything", Toast.LENGTH_SHORT).show();
                ingredients.moveToFirst();

                String ingredient = ingredients.getString(ingredients.getColumnIndex("ingredient_name"));
                int measurement = ingredients.getInt(ingredients.getColumnIndex("measurement"));
                adapter.deductIngredient(ingredient, measurement);

            }
            else
            {

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        context);

                // set title
                alertDialogBuilder.setTitle("Uh Oh!");

                // set dialog message
                alertDialogBuilder
                        .setMessage("Opps looks like yuor out of some stuff, Want to add it to your shopping list?")
                        .setCancelable(false)
                        .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                final Cursor missing2 = adapter.missingIngredients(recipeCode);
                                while(missing2.moveToNext()) {
                                    String n = missing2.getString(missing2.getColumnIndex("ingredient_name"));
                                    Toast.makeText(getApplicationContext(), "Adding Missing to SHopping List: " + n, Toast.LENGTH_SHORT).show();
                                    adapter.insertItem(n, 100, "grams");
                                }
                                SingleRecipeDisaply.this.finish();
                            }
                        })
                        .setNegativeButton("No",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                // if this button is clicked, just close
                                // the dialog box and do nothing
                                dialog.cancel();
                            }
                        });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();
            }
        }
    });

为什么你在 onClick 中添加了 finish(),这一定是导致问题的原因,你的 activity 也必须关闭,而不仅仅是 AlertDialog

@Override
public void onClick(View v) {
    finish();
    ...
    ...

删除 finish() 就可以了。