2016-02-05 46 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