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.