You need to call the parts of code which modify the UI inside the main thread of the activity.
To do this use runOnUiThread
to call setListAdapter(adapter)
:
runOnUiThread(new Runnable() {
@Override
public void run() {
setListAdapter(adapter)
}
});
Do the same with the result.setText("Error");
.
EDIT:
private void takeXML()
{
new Thread(new Runnable() {
public void run()
{
try{
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
String adresxml="http://xxx.xml";
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(adresxml); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
for (int i = 0; i < nl.getLength(); i++)
{
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_COST, parser.getValue(e, KEY_COST));
// adding HashList to ArrayList
// excel[i] = new ExcelFileClass()
menuItems.add(map);
}
Log.v("List","Array:"+menuItems);
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(getBaseContext(), menuItems,R.layout.list_item,new String[] { KEY_NAME, KEY_COST, KEY_ID }, new int[] {R.id.name,R.id.cost, R.id.elements });
runOnUiThread(new Runnable() {
@Override
public void run() {
setListAdapter(adapter)
}
});
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
......
}
});
}catch(Exception e){
//runOnUiThread(new Runnable() {public void run() {p.dismiss();}});
runOnUiThread(new Runnable() {
@Override
public void run() {
result.setText("Error");
}
});
System.out.println("Exception : " + e.getMessage());
}
}
}).start();
}
Hope it helps :)