单击AlertDialog按钮后显示ProgressDialog

时间:2013-02-25 19:24:35

标签: android progressdialog

我有AlertDialog我希望在点击ProgressDialog正面按钮后显示AlertDialog(在互联网连接期间 - 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();

      }
    }); 

DownloadXmlTask​​ 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();
}

}

1 个答案:

答案 0 :(得分:2)

从asynctask调用中删除.get()。在OnPostExecute中使用getSnippet()。