2

I need to parse an xml file which takes about 3 seconds, and immediately use the data that is parsed from the xml file after it has completed. However, since it takes around 3 seconds to complete, i don't just want the screen sitting and waiting for 3 seconds showing nothing. I would like it to display a ProgressDialog. I've tried everything in these threads and while most do 2 / 3, none seem to do all 3.

Basically I have a button that when pressed, i want to parse the xml file and then use the data parsed from the file:

void onButtonClicked() {

    ProgressDialog pd = new ProgressDialog(this);
    pd.setMessage("Parsing...");
    pd.show();
    String[] ret;
    return = parseXmlFile();
    pd.dismiss();
    if (ret[0] == "steve") {
       sayHiToSteve();
    }
}  

If I use an asyncTask, it will continue on with execution and check ret[0] before the task is completed and will just be null. if i do AsyncTask.get(), it pauses the execution but doesn't display the progress dialog, so that's no good either.

I also looked at creating a seperate thread, but i had the same problem. I can't use a timer, because it is about 3 seconds (but can be up to 4 or 5 seconds if connection is slow), so i can't approximate.

Any help is much appreciated. Thanks in advance.

user1154920
  • 465
  • 1
  • 6
  • 21

2 Answers2

2

You can use an AsyncTask for this, something like:

public class ParseXMLTask extends AsyncTask<Void, Void, String[]>
{

    private ProgressDialog dlg;

    @Override
    protected void onPostExecute(String[] result)
    {
        dlg.dismiss();
        if (result[0] == "steve")
        {
            sayHiToSteve();
        }

        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute()
    {
        dlg = new ProgressDialog(MyActivity.this);
        dlg.setMessage("Parsing...");
        dlg.setCancelable(false);
        dlg.setIndeterminate(true);
        dlg.show();

        super.onPreExecute();
    }

    @Override
    protected String[] doInBackground(Void... params)
    {
        return parseXmlFile();
    }

}

then to use it you do:

void onButtonClicked() 
{
    ParseXMLTask tsk = new ParseXMLTask();
    tsk.execute((Void)null);
}
John Boker
  • 82,559
  • 17
  • 97
  • 130
  • Is there a way to wait until tsk.execute((Void)null) finishes before continuing on? Like i want to do ParseXMLTask tsk = new ParseXMLTask(); tsk.execute((Void)null); String[] ret = tsk.getData(); if (ret == "") { log.e("data wasn't finished yet"); } but the execution will continue and hit the getStatement and the if, before the parsing has finished – user1154920 Jan 17 '12 at 21:53
  • 2
    What you'll need to do is anything you want to do after the parseXmlFile() you need to put in onPostExecute(). the doInBackground() executes on a separate thread and does not lock up the UI, when it's finished it calls the onPostExecute() back on the main thread. – John Boker Jan 17 '12 at 22:01
0

How about a join in the main thread.

See thread join not working in Android? and http://docs.oracle.com/javase/tutorial/essential/concurrency/join.html

Community
  • 1
  • 1
Usman Ismail
  • 17,999
  • 14
  • 83
  • 165