-2

I have looked through a lot of content online but none of the suggestions work. I have a listview that sometimes work and sometimes crashes my app with the following:

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. Make sure your adapter calls notifyDataSetChanged() when its content changes.

I am definitely calling the notifyDataSetChanged() (In the onPostExecute() of an async task). I have tried as suggested by examples online to run it on the main thread like below:

getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    CAdapterFilter.notifyDataSetChanged();
                }
            });

but that did not work either. Still my app will crash at random times. Can anyone shed some light here? Why is it only crashing randomly and not every time. What am I missing?

Christopher Smit
  • 953
  • 11
  • 27

1 Answers1

1

Try this :-

doInBackground(....) {
 mPseudoList.addAll(fetched_list);
 return xyz;
}

onPostExecute(...) {
 mAdapterList.addAll(mPseudoList);
 mAdapter.notifyDataSetChanged();
}

Change the adapter list reference in onPostExecute(...) and then notify the adapter !!

Note :-

In doInBackground dont update the list whose reference the adapter holds , instead use a pseudo-list and update the adapter reference list in onPostExecute

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52