android async load list from xml
<p>i'm loading a xml data from url and bind it to a list.<br>
the process take some second and the view is freze.<br>
i want to implement the code with async task so it will load in background or show a progress dialog.</p>

<pre><code>import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;

public class XmlActivity extends ListActivity {
    private class LoadDataTask extends AsyncTask&lt;Object, Object, Object&gt; {
        @Override
        protected Object doInBackground(final Object... urls) {
            fillData();

            return null;
        }

        @Override
        protected void onPostExecute(final Object result) {
            closeDialog();
        }

        @Override
        protected void onPreExecute() {
            showDialog();
        }
    }

    private NumbersDataSource datasource;
    private ProgressDialog dialog;

    public void fillData() {

        datasource = new NumbersDataSource(this);
        datasource.open();

        List&lt;Number&gt; values = datasource.getAllNumbers();
        datasource.close();

        ArrayList&lt;HashMap&lt;String, String&gt;&gt; mylist = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();

        String xml = XMLfunctions
                .getXML("http://www.xxxxxx.com/products.xml");
        Document doc = XMLfunctions.XMLfromString(xml);

        NodeList nodes = doc.getElementsByTagName("item");

        String temp = "";

        for (int i = 0; i &lt; nodes.getLength(); i++) {
            HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();

            Element e = (Element) nodes.item(i);
            map.put("category", XMLfunctions.getValue(e, "category"));
            map.put("title", XMLfunctions.getValue(e, "title"));
            map.put("description", XMLfunctions.getValue(e, "description"));
            map.put("pubDate", XMLfunctions.getValue(e, "pubDate"));
            map.put("link", XMLfunctions.getValue(e, "link"));

            String str2array = XMLfunctions.getValue(e, "description");
            String[] arr = str2array.split(",");

            String tempResults = "";
            for (int x = 0; x &lt; arr.length; x++) {
                String tempWin = arr[x].trim();

                Iterator&lt;Number&gt; itr = values.iterator();
                while (itr.hasNext()) {

                    Number number = (Number) itr.next();

                    tempResults += number.hasWin(tempWin);

                }

            }
            if (tempResults.equals(""))
                map.put("results", getText(R.string.no_win).toString());
            else
                map.put("results", getText(R.string.win).toString());

            if (temp == "") {
                temp = XMLfunctions.getValue(e, "pubDate");
            }

            if (temp.equals(XMLfunctions.getValue(e, "pubDate"))) {
                mylist.add(map);
            }

        }

        ListAdapter adapter = new SimpleAdapter(this, mylist,
                R.layout.xml_row_list, new String[] { "category", "title",
                        "description", "pubDate", "results" }, new int[] {
                        R.id.item_category, R.id.item_title,
                        R.id.item_description, R.id.item_pubDate,
                        R.id.item_link });

        setListAdapter(adapter);

    }

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xml_list);
        // fillData();
        new LoadDataTask().execute();

    }

    @Override
    protected void onResume() {
        super.onResume();
        // fillData();
    }

    private void closeDialog() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    private void showDialog() {
        if (dialog != null &amp;&amp; !dialog.isShowing()) {
            dialog = new ProgressDialog(this);
            dialog.setMessage("Loading. Please wait...");
            dialog.show();
        }
    }
}
</code></pre>

<p>the  ProgressDialog  in fillData is never shown.
and i get an error</p>

<pre><code>03-19 21:13:38.535: E/AndroidRuntime(1669): FATAL EXCEPTION: AsyncTask #2
03-19 21:13:38.535: E/AndroidRuntime(1669): java.lang.RuntimeException: An error occured while executing doInBackground()
03-19 21:13:38.535: E/AndroidRuntime(1669):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at java.lang.Thread.run(Thread.java:1019)
03-19 21:13:38.535: E/AndroidRuntime(1669): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
03-19 21:13:38.535: E/AndroidRuntime(1669):     at android.view.ViewRoot.checkThread(ViewRoot.java:3035)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at android.view.ViewRoot.requestLayout(ViewRoot.java:637)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at android.view.View.requestLayout(View.java:8320)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at android.view.View.requestLayout(View.java:8320)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at android.view.View.requestLayout(View.java:8320)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at android.view.View.requestLayout(View.java:8320)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at android.view.View.requestLayout(View.java:8320)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at android.view.View.requestLayout(View.java:8320)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at android.view.View.requestLayout(View.java:8320)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at android.view.View.requestLayout(View.java:8320)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at android.view.View.requestLayout(View.java:8320)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at android.view.View.setFlags(View.java:4694)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at android.view.View.setVisibility(View.java:3169)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at android.widget.AdapterView.updateEmptyStatus(AdapterView.java:727)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at android.widget.AdapterView.checkFocus(AdapterView.java:697)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at android.widget.ListView.setAdapter(ListView.java:486)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at android.app.ListActivity.setListAdapter(ListActivity.java:265)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at com.balabush.pais.XmlActivity.fillData(XmlActivity.java:109)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at com.balabush.pais.XmlActivity$LoadDataTask.doInBackground(XmlActivity.java:23)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-19 21:13:38.535: E/AndroidRuntime(1669):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
03-19 21:13:38.535: E/AndroidRuntime(1669):     ... 4 more
</code></pre>
