0

I have an xml parser that parses a large file with 1056 tags of info I am grabbing locally (not url based).

I would like to have a dialog show while this thing is running through the for loop, but instead i just see a white screen until all is done loading. I've tried adding a dialog.show() in a few different spots, even right onCreate without any success. What am i missing here? I've tried running this method in Handler and Async, but get the Not On Main Thread error each time... How do you run long tasks with a dialog on the main thread?

In my onCreate I just do (along with the other normal layout, actionbar etc...):

buildMap();

Here is my method:

public void buildMap() {
        try {

            File fXmlFile = new File(
                    "/storage/emulated/0/snoteldata/kml/snotelwithlabels.kml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);

            doc.getDocumentElement().normalize();

            NodeList nList = doc.getElementsByTagName("Placemark");
            int temp;
            for (temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;

                    String name = eElement.getElementsByTagName("name").item(0)
                            .getTextContent();

                    Spanned desc = Html.fromHtml(eElement
                            .getElementsByTagName("description").item(0)
                            .getTextContent());

                    String lon = eElement.getElementsByTagName("longitude")
                            .item(0).getTextContent();

                    String lat = eElement.getElementsByTagName("latitude")
                            .item(0).getTextContent();

                    lon = lon.trim();
                    lat = lat.trim();

                    double lati = Double.parseDouble(lat);
                    double lngi = Double.parseDouble(lon);

                    LatLng LOCATION = new LatLng(lati, lngi);

                    map.addMarker(new MarkerOptions()
                            .position(LOCATION)
                            .title(name)
                            .snippet(desc.toString())
                            .icon(BitmapDescriptorFactory
                                    .fromResource(R.drawable.wfmi_icon48)));
                    map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
                        @Override
                        public void onInfoWindowClick(Marker arg0) {
                            msg(arg0.getSnippet());
                        }
                    });
                }

            }

            showLocation();

        } catch (Exception e) {
            Log.e("SnoTelData Buildmap Error", e.getMessage());
        }
    }

How do I show that this for loop is running, and to wait a sec?

EDIT::::

Thanks for the responses. I have tried what has been proposed without success. I get the Not on Main Thread error.

Here is my async task:

private class BuildTask extends AsyncTask<String, Void, Void> {
        @Override
        protected Void doInBackground(String... params) {
            buildMap();
            return null;
        }

        protected void onPreExecute() {
            dialog.show();
         }

         protected void onPostExecute(Void result) {
            dialog.cancel();
         }
    }

and I call if from the onCreate:

new BuildTask().execute();

Why am i getting the not on main thread error still? I am executing this from the onCreate.

jasonflaherty
  • 1,924
  • 7
  • 40
  • 82

4 Answers4

0

Use an AsyncTask. In the doInBackground() method buildMap(). In the onPreExecute() and onPostExecute() methods show and hide the dialog.

     protected void onPreExecute() {
        //Show Dialog
     }

     protected void onPostExecute(Void result) {
         //Dismiss Dialog
     }
nedaRM
  • 1,837
  • 1
  • 14
  • 28
0

Use a Thread, possibly an AsyncTask. And run the buildMap() method in the thread. create a ProgressDialog before calling the method and show this ProgressDialog on UI thread. and when all yor parsing is done, just dismiss the ProgressDialog ( on UI thread).

AnujMathur_07
  • 2,586
  • 2
  • 18
  • 25
0

use AsyncTask and show your dialogue in onPreExecute() and dismissDialog() in onPostExecute()

Digit
  • 1,929
  • 1
  • 14
  • 17
0

You have to do the parsing in the background in a separate thread. This thread then sends update messages to your main thread where the progress is being shown.

I have created a ProgressDialog class which takes care of all this

public class ProgressDialogForParser extends
    DialogFragment {

It has a static handler called: theHandlerForScreenUpdate:

public static Handler theHandlerForScreenUpdate;

This is how to initialize it:

public final Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
    updateProgressDialog(msg); // here you do whatever you like to update the screen
}
};

And this is a static method that you can use easily from your parser thread to update the screen progress. Additionally I pass some information to the handler, so it knows what it should do, in case you need this too.

   static public void sendMessageToProgressDialog(
        int msgID, String msgText, boolean statusBool) {
    if (theHandlerForScreenUpdate == null)
        return;
    Message msgForProgressDialogDuringConnectionTest = theHandlerForScreenUpdate
        .obtainMessage(msgID);
    Bundle bundle = new Bundle();
    bundle.putBoolean(msgText, statusBool);
    msgForProgressDialogDuringConnectionTest.setData(bundle);
    theHandlerForScreenUpdate
        .sendMessage(msgForProgressDialogDuringConnectionTest);

    }
user387184
  • 10,953
  • 12
  • 77
  • 147