2016-02-05 40 views
0

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

是否有某种计时器,我可以放在警告框,以保持它打开或我不正确地实施框?

按钮W/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(); 
      } 
     } 
    }); 
+0

你提到了一个for循环?代码在哪里? – lase

+0

添加循环代码。 – Rohit5k2

+0

@ Rohit5k2循环可能是正面的按钮内部的while循环 – Stallion

回答

0

你为什么在onClick添加finish(),必须引起问题,你的活动也必须关闭,而不仅仅是AlertDialog

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

删除finish()它会工作。

+0

良好的渔获物。这可能是。 – Rohit5k2

+0

请在问题的评论中提问而不是回答... – Mohit

+0

@Mohit:这是一个答案张贴为一种问题。 :D – Rohit5k2