I want a progress dialog saying “Please Wait” , while my code downloads and parses a xml file but the Progress Dialog is not going, it somehow getting stuck.
I am using AsyncTask, downloading of xml file and parsing are done in the background thread, the code is working if i remove the progress dialog, but in that case you have to wait for about 4 sec to click my button that dynamically creates UI.
private class ParseXML extends AsyncTask<Integer, Integer, Document>{
private Context context;
private Activity activity;
private ProgressDialog pd;
public ParseXML(Activity activity) {
this.activity = activity;
context = activity;
pd = new ProgressDialog(context);
}
public void onPreExecute(){
super.onPreExecute();
/* ProgressDialog pd=ProgressDialog.show(XML_PARSER.this,"","Please Wait");*/
}
@Override
protected Document doInBackground(Integer... params) {
DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
Document dom1=null;
try {
//InputStream is=getResources().openRawResource(R.raw.options);
URL requestURL = new URL("my url");
URLConnection connection = requestURL.openConnection();
is = connection.getInputStream();
DocumentBuilder db = dbf.newDocumentBuilder();
dom1=db.parse(is);
Log.i(TAG,"parsing done");
}
catch(ParserConfigurationException pce){
pce.printStackTrace();
}
catch(SAXException se){
se.printStackTrace();
}
catch(IOException ioe){
ioe.printStackTrace();
}
ParseDocument(dom1);
return null;
}
@Override
public void onPostExecute(Document d){
if(pd!=null && pd.isShowing()){
pd.dismiss();
}
super.onPostExecute(d);
}
}
You are making mistake here
you are declaring
ProgressDialog pda local variableand when control is coming to
pd is null here as you have not initialize the global pd variable. that is why alert dialog is not being dismissed.
to make this work just make some changes in
onPreExecute