0

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.

SubbaReddy PolamReddy
  • 2,083
  • 2
  • 17
  • 23
pathfinder
  • 39
  • 3
  • I somehow doubt that exception comes from showing the `ProgressBar`. Change your task so the line `whatToDoInfo.add(map)` is run in the `onProgressUpdate` method and see if you have the same problem. – user Nov 17 '12 at 09:13

1 Answers1

0

Just to check: you set your adapter to your listview before you call WhatToDoLoader, right?

Otherwise it may occur that your onProgressUpdate method get called before you set your adapter to listview and your list's not get notified... So this error can happen.

yahya
  • 4,810
  • 3
  • 41
  • 58