我有AlertDialog
并且我想ProgressDialog
在单击AlertDialog
肯定按钮后显示(在 Internet 连接期间 - AsyncTask
)。问题是进度对话框仅在完成任务后以及何时AlertDialog
关闭时才显示。我试图隐藏()或解雇()AlertDialog
,但它不起作用。
知道如何在 AlertDialog 前面显示 ProgressDialog 吗?这是我的代码。
LayoutInflater li = LayoutInflater.from(kontekst);
final View vNearest;
vNearest = li.inflate(R.layout.nearest, null);
AlertDialog.Builder builder = new AlertDialog.Builder(kontekst);
builder.setView(vNearest);
builder.setCancelable(true);
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int witch) {
String xml = "www.example.com";
MarkerOptions nearestMO = new DownloadXmlTask().execute(xml).get();
String distance = nearestMO.getSnippet();
}
});
下载XmlTask AsyncTask:
private class DownloadXmlTask extends AsyncTask<String, Integer, MarkerOptions> {
ProgressDialog waitDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
waitDialog = ProgressDialog.show(Home.this, "Working", "Please Wait", true, true);
waitDialog.setCanceledOnTouchOutside(false);
}
@Override
protected MarkerOptions doInBackground(String... urls) {
try{
URL xmlURL= new URL(urls[0]);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
HandlingXMLStuff readingXML = new HandlingXMLStuff();
xr.setContentHandler(readingXML);
xr.parse(new InputSource(xmlURL.openStream()));
return readingXML.getNearest();
} catch (Exception e) {
return null;
}
}
protected void onPostExecute(MarkerOptions result) {
super.onPostExecute(result);
waitDialog.dismiss();
}
}