Progressbar blocks notiyfyDatasetChanged() method in Android

Posted by pathfinder on Stack Overflow See other posts from Stack Overflow or by pathfinder
Published on 2012-11-17T04:55:55Z Indexed on 2012/11/17 4:59 UTC
Read the original article Hit count: 422

I'm trying to display a ProgressBar while a listview is being populated.

This is my XML

 <FrameLayout

    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:fadingEdge="none" >
    </ListView>

    <ProgressBar
        android:id="@+id/doProgress"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="center" />
</FrameLayout>

ProgressBar's visibiliy has been changed in the onPostExecuteMethod when the whole listview is loaded. AsyncTask Code:

public class WhatToDoLoader extends AsyncTask<String, String, String> {
    ProgressDialog progress = new ProgressDialog(WhatToDo.this);
    String url = "http://wearedesigners.net/clients/clients12/tourism/fetchWhatToDoList.php";
    final String TAG_MAIN = "item";
    final String TAG_ID = "itemId";
    final String TAG_NAME = "itemName";
    final String TAG_DETAIL = "itemDetailText";
    final String TAG_ITEM_IMAGE = "itemImages";
    final String TAG_MAP = "itemMapData";
    final String TAG_MAP_IMAGE = "mapImage";

    @Override
    protected void onProgressUpdate(String... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        /*
         * progress.setMessage("Loading What To Do List"); progress.show();
         */

    }

    @Override
    protected String doInBackground(String... params) {
        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(url); // getting XML
        Document doc = parser.getDomElement(xml); // getting DOM element
        NodeList nl = doc.getElementsByTagName(TAG_MAIN);
        // TODO Auto-generated method stub
        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(TAG_ID, parser.getValue(e, TAG_ID));
            map.put(TAG_NAME, parser.getValue(e, TAG_NAME));

            map.put(TAG_DETAIL, parser.getValue(e, TAG_DETAIL));

            map.put(TAG_MAP, parser.getValue(e, TAG_MAP));
            map.put(TAG_MAP_IMAGE, parser.getValue(e, TAG_MAP_IMAGE));
            map.put(TAG_ITEM_IMAGE, parser.getValue(e, TAG_ITEM_IMAGE));
            System.out.println("Test : " + parser.getValue(e, TAG_ID));
            // adding HashList to ArrayList
            whatToDoInfo.add(map);
            publishProgress("");
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        ProgressBar pb = (ProgressBar) findViewById(R.id.doProgress);
        pb.setVisibility(pb.INVISIBLE);
    }

}

When i run the code it throws the following exception.

*java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread.*

But the same code works fine when the progressbar feature is omitted. I can't find where i'm going wrong. can someone please help me ?

Thank you in advance.

© Stack Overflow or respective owner

Related posts about android

Related posts about android-asynctask