Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 :)

share|improve this question
    
provide your URL. –  Dhawal Sodha Parmar Oct 30 '12 at 8:59
    
Make a custom adapter that holds the arraylist, use notifydatasetchanged to update ui based on adapters arraylist –  user1281750 Oct 30 '12 at 9:06
2  
If the app fails then you should post the exception/error you get when running the application. Looking at your code I see you use this as a reference to a Context in the onPostExecute method, but as the onPostExecute method is in the AsyncTask class this will point to the current task instance and NOT a valid Context. So obtain a valid Context reference for the ArrayAdapter(for example, if the task is a inner class in the activity you could use YourActivityName.this). –  Luksprog Oct 30 '12 at 9:09
    
I used the luksprog reply and this thread [link]stackoverflow.com/questions/8034587/… to figure out a solution that worked with updating the listview on the ui. –  Anders Ilenkop Oct 31 '12 at 10:29
    
I did move the arraylist up to class level and changed the arrayadapter in postexecute ArrayAdapter adapter = new ArrayAdapter(Forside.this, R.layout.list_item, menuItems); setListAdapter(adapter); –  Anders Ilenkop Oct 31 '12 at 10:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.