I'm writing an Activity which fetches some XML from the web via HTTP and then parses it into a DOM. It then pulls some required data from the DOM.
As you can imagine, this takes a few moments, so I put that code into it's own thread and then tried to set up a ProgressDialog to display while the user is waiting for that to complete.
The problem is that the ProgressDialog doesn't display at all. If I remove the call to dismiss() then it displays after the work is done and, obviously, just sits there...
Here is my code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
progressDialog = ProgressDialog.show(this, "", "Loading...", true);
Thread downloadThread = new Thread(new DownloadVerseThread());
downloadThread.start();
setContentView(R.layout.main);
TextView verseView = (TextView) findViewById(R.id.displayFighterVerse);
TextView dateView = (TextView) findViewById(R.id.displayDateRange);
TextView referenceView = (TextView) findViewById(R.id.displayReference);
try {
downloadThread.join();
} catch (InterruptedException e) {
Log.e(TAG, "Attempted to set the UI values before we were ready.");
return;
}
Log.d(TAG, "Date: " + date);
Log.d(TAG, "Reference: " + reference);
Log.d(TAG, "Scripture: " + scripture);
progressDialog.dismiss();
dateView.setText(date);
referenceView.setText(reference);
verseView.setText(scripture);
}
private class DownloadVerseThread implements Runnable {
@Override
public void run() {
try {
FighterVerseDownloader downloader = new FighterVerseDownloader();
date = downloader.getDateRange();
reference = downloader.getReference();
scripture = downloader.getScripture();
} catch (FighterVersesException e) {
Log.e("FighterVerses", "Failed to get the Fighter Verse", e);
}
}
}
The constructor of the FighterVersesDownloader is where all the work in terms of HTTP and DOM is done.
Any ideas on what I'm missing? I've seen a few similar threads on this using AsyncTask, but I wanted to use good old fashioned Threads directly and the solutions suggested don't apply.