I have the following Java method for parsing XML data:
public static List<String> getStations(){
List<String> stnames = new ArrayList<String>();
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = (Document) docBuilder.parse(new URL("http://api.irishrail.ie/realtime/realtime.asmx/getAllStationsXML_WithStationType?StationType=D").openStream());
doc.getDocumentElement().normalize();
NodeList stationames = doc.getElementsByTagName("objStation");
for(int i = 0; i<stationames.getLength(); i++){
Element firstStat = (Element) stationames.item(i);
Element stationalias = (Element) firstStat.getElementsByTagName("StationDesc").item(0);
stnames.add(stationalias.getTextContent().trim());
}
} catch (SAXParseException err) {
System.out.println("** Parsing error" + ", line "
+ err.getLineNumber() + ", uri " + err.getSystemId());
System.out.println(" " + err.getMessage());
} catch (SAXException e) {
Exception x = e.getException();
((x == null) ? e : x).printStackTrace();
} catch (Throwable t) {
t.printStackTrace();
}
return stnames;
}
As you can see the method returns a List of Strings. This works perfectly on Eclipse, as I can print the List out on the console, using System.out.println(getStations());
Now, I copied and pasted this method into my Android program. Clearly the method returns a List of Strings which is a requirement for setting up an Array Adapter for a List View in android.
So I have the following android code:
List<String> sts = getStations();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
sts );
lv.setAdapter(arrayAdapter); // where lv is my List View
Running this code gives me a blank List View. I am running the android project on my Android Phone through a USB cable. I do have WIFI on my Android device, I have also pasted the following permission to my manifest file:
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
...
...
So, does anyone have any idea why my List View is not showing any information?
It is driving me mad, I've been working on this simple problem for hours.
I am also having problems debugging using the Log.e()
method so I can't provide any information there.