In this case, you should use AsyncTaskLoader . This will simplify the situation. Just create your own class that extends AsyncTaskLoader and just has one method, loadInBackground() . In your loadInBackground() method, enter the code you use to parse the xml as follows:
public List<String> loadInBackground(){ ArrayList toReturn = new ArrayList<String>(); try { URL url = new URL("http://www.bnr.ro/nbrfxrates.xml"); URI uri = new URI(url.toString()); HttpGet httpget = new HttpGet(uri); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(httpget); if (response.getStatusLine().getStatusCode()==200) { url = new URL("http://www.visual-efx.net/nbrfxrates.xml") } SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); ExampleHandler myExampleHandler = new ExampleHandler(); xr.setContentHandler(myExampleHandler); xr.parse(new InputSource(url.openStream())); for (int i=1; i<=31; i++){ toReturn.add(myExampleHandler.valuta[i] + " = " + myExampleHandler.pret[i] + " lei"); } } catch (Exception e) { Log.e(MY_DEBUG_TAG, "Error", e); } return toReturn; }
Then, in the action in which you display the list, do your activity in the LoaderManager.LoaderCallbacks interface, for example
public YourActivity implements LoaderManager.LoaderCallbacks<List<String>>
Then follow the callback methods. For example, your onLoadFinished() method would look something like this:
public void onLoadFinished(Loader<List<String>>, List<String> data){ aa.setData(data); }
For more information about bootloaders, check this documentation . Please note: if you focus on API less than 3.0, you need to use the Android Support Package .
Kurtis nusbaum
source share