正如标题所示,我的应用程序中的警报框不会保持打开状态。该框嵌套在for循环中,单击按钮时会检查该循环。当循环进入警告框时,它会在屏幕上闪烁,然后立即消失。
我可以在警告框上放置某种计时器以使其保持打开状态,或者我是否错误地实施了该框?
按钮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 :(得分:0)
为什么您在finish()
中添加了onClick
,这必定会导致问题,您的活动也必须关闭,而不仅仅是AlertDialog
。
@Override
public void onClick(View v) {
finish();
...
...
删除finish()
即可。