I am making an app which is polling an XML sheet from a url through AsyncTask, so it does'nt interfere with the mainThread.
I have been able to make the connection, retrieve the xml and parse the values in a loop and add these to an arraylist.
My problem is that when the app is loading on the handset, the arraylist should be transformed to an arrayadapter and then populate the listview in the app.
I've got it to work with an hardcoded xml file, but when I try to use the parsed values from my dynamic xml, then the app fails.
This code is getting the XML sheet and adds it to an arraylist "menuItems" - this works fine
@Override
protected Boolean doInBackground(String... params) {
Log.d("beskeden", "Henter adressen: " + URL);
try{
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_NAVN, nl.item(i).getChildNodes().item(1).getTextContent());
map.put(KEY_BESKRIVELSE, nl.item(i).getChildNodes().item(1).getTextContent());
map.put(KEY_THETYPE, nl.item(i).getChildNodes().item(1).getTextContent());
map.put(KEY_THETIMER, nl.item(i).getChildNodes().item(1).getTextContent());
map.put(KEY_THEGRADER, nl.item(i).getChildNodes().item(1).getTextContent());
map.put(KEY_THESEMERE, nl.item(i).getChildNodes().item(1).getTextContent());
map.put(KEY_IDP, nl.item(i).getChildNodes().item(1).getTextContent());
Log.e("Beskeden", nl.item(i).getChildNodes().item(1).getNodeName() + ": " + nl.item(i).getChildNodes().item(1).getTextContent());
Log.e("Beskeden", nl.item(i).getChildNodes().item(3).getNodeName() + ": " + nl.item(i).getChildNodes().item(3).getTextContent());
Log.e("Beskeden", nl.item(i).getChildNodes().item(5).getNodeName() + ": " + nl.item(i).getChildNodes().item(5).getTextContent());
Log.e("Beskeden", nl.item(i).getChildNodes().item(7).getNodeName() + ": " + nl.item(i).getChildNodes().item(7).getTextContent());
menuItems.add(map);
}
Log.d("beskeden", "Antal theer: " + nl.getLength());
Log.d("beskeden", "Xml er blevet parset");
return true;
} catch (Exception e){
return false;
}
}
The following code is doing so i can't compile and test it - so frustating :/
//@Override
protected void onPostExecute(final Boolean success) {
//super.onPostExecute(result);
Log.d("Besked", "Post executen");
if (success) {
Log.d("Beskeden", "SUCCES!!");
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.list_item, menuItems);
}else{
Log.d("Beskeden", "FAIL!!");
}
I am a bit stuck, so any help is welcome :)
this
as a reference to aContext
in theonPostExecute
method, but as theonPostExecute
method is in theAsyncTask
classthis
will point to the current task instance and NOT a validContext
. So obtain a validContext
reference for theArrayAdapter
(for example, if the task is a inner class in the activity you could useYourActivityName.this
). – Luksprog Oct 30 '12 at 9:09