显然我有一个我喜欢称之为“鸡或鸡蛋”综合症的案例。我试图找到一种方法来杀死一个警报对话框,该对话框直到过程结束才会创建。
我想做什么:当被问到“你想重新下载这个画廊吗?”时,我想在得到“是”答案时杀死底层弹出窗口(警报/构建器)。但是当我得到“否”答案时。换句话说,我想在那里放一个alert.dismiss()......但我不能,因为它不会在流程结束之前创建。
这有意义吗?我试图找到另一种方式向用户展示这个问题,我正在绞尽脑汁。
这是我的代码:
public void showGalleriesDialogue(String galleriesArray) {
Log.v("Status","Ran alert dialogue routine.");
final CharSequence[] items = TextUtils.split(galleriesArray, "\\^");
final AlertDialog.Builder builder = new AlertDialog.Builder(MainMenu.this);
builder.setTitle("Choose a Gallery to download:");
builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
final String galleryToDownload = (String) items[item].toString();
// Check to ensure that this doesn't all ready exist
File checkExisists = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/pm-pvault/" + galleryToDownload);
Log.v("Status","Checking to ensure that '"+checkExisists+"' does not exist.");
if(checkExisists.exists()) {
AlertDialog.Builder existsBuilder = new AlertDialog.Builder(MainMenu.this);
existsBuilder.setTitle("But wait!");
existsBuilder.setMessage("This gallery already exists. Do you want to RE-INSTALL it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Preview stuff, then go get it.
GlobalDataStore.retrycount = 0;
preview(galleryToDownload);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Never mind.
dialog.cancel();
}
});
AlertDialog existsAlert = existsBuilder.create();
existsAlert.show();
}
//OR ELSE
else
if(galleryToDownload.contains("@")==true)
{
AlertDialog.Builder doItNasty = new AlertDialog.Builder(MainMenu.this);
doItNasty.setTitle("DISCLAIMER");
doItNasty.setMessage(R.string.nastyDisclaimer)
.setCancelable(false)
.setPositiveButton("I AGREE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Download stuff, go get it.
GlobalDataStore.retrycount = 0;
preview(galleryToDownload);
}
})
.setNegativeButton("DISAGREE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Never mind.
dialog.cancel();
}
});
AlertDialog nastyAlert = doItNasty.create();
nastyAlert.show();
}
else {
// Out of IF conditions to meet. Go download it now.
GlobalDataStore.retrycount = 0;
preview(galleryToDownload);
}
} // END OR ELSE
}
});
final AlertDialog alert = builder.create();
alert.show();
}
答案 0 :(得分:2)
为什么不在用户选择是或否时设置标志,然后在构建对话框时检查您的标志,如果用户选择是,则不创建它。这样你就不必担心因为你没有成功而对它进行解雇。
答案 1 :(得分:0)
onClick事件是一个回调,这意味着在调用它时对话框已经存在,因此取消它时应该没有问题。
问题是你正在重新定义变量'对话',所以你无法访问'外部'对话框。
尝试这样做:
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface redownloadDialog, int id) {
// Never mind.
redownloadDialog.cancel();
dialog.cancel();
}
});