At the moment I have 14 elements added to an array list but I want them to be displayed in a listview activity so far I have:
public class ListView extends ListActivity{
static final String baseURL ="http://www.dublincity.ie/dublintraffic/cpdata.xml?1354720067473";
TextView tvcp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
StringBuilder URL = new StringBuilder(baseURL);
String fullURL = URL.toString();
try{
URL website = new URL(fullURL);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
HandelingXML gettingData = new HandelingXML();
xr.setContentHandler(gettingData);
xr.parse(new InputSource(website.openStream()));
ArrayList<CarparkNode>carparks = gettingData.getCarparks();
this.setListAdapter(
new ArrayAdapter<CarparkNode>(ListView.this,R.layout.listview, android.R.id.list, carparks)
);
}catch(Exception e){
tvcp.setText("error");
}
}
}
However, every time I run this it will crash and I have no idea why as i have done this before using a simple array. If someone could point out what i'm doing wrong and give a solution that would be great thanks!
e.printStackTrace()to you catch block and read through the new stack trace, you might see a NetworkOnMainThreadException. After you've done this, click edit to post the warnings and errors you see so we don't have to guess.Main Thread. You need to move your parsing into background Thread for exampleAsyncTask- that also allowsUIupdates after work is done.