0

The exception suggests me to call the notifyDataSetChanged(), but I already call that method on my onPostExecute method, as you can see:

Constants.newsDownloaded = true;
TempStorage.downloadedNews = this.items;
this.progress.setVisibility(View.INVISIBLE);
this.downloadMore.setVisibility(View.VISIBLE);
adapter = new NewsListAdapter(context,
                inflater, this.items);

if (index <= 26){
    listView.setSelection(0);
} else{
    listView.setSelection(TempStorage.firstAddedIndex);
}

listView.setAdapter(adapter);
adapter.notifyDataSetChanged();

The adapter is recreated at every call as you can see. I've got this problem only on the Nexus 5, on the others devices works flawlessly.

What's wrong with this code?

Thanks for the help, guys!

Pipodi
  • 49
  • 1
  • 8
  • I think you might wanna check this thread: http://stackoverflow.com/questions/3132021/android-listview-illegalstateexception-the-content-of-the-adapter-has-changed?rq=1 – peresisUser Mar 29 '15 at 11:25
  • @peresisUser I checked it. But, as the author of the thread suggests, I always call the notifyDataSetChanged() everytime, just because the fact of being in the onPostExecute it means that the notifyDataSetChanged() method is called everytime... – Pipodi Mar 29 '15 at 11:41
  • Please post your code – Shriram Mar 29 '15 at 11:42
  • @Shriram The code is on the question. That code is from my onPostExecute AsyncTask. – Pipodi Mar 29 '15 at 11:50

1 Answers1

1

I think you should delete the notifyDataSetChanged line. You are creating a new adapter instance anyway, why should you nodify it right after? it has the correct updated data, because you have a new instance.

I used to get the same crash in a similar case, in updating data i was querying for in an adapter. By just creating a new adapter instance in my onPostExecute, i got rid of the crash.

peresisUser
  • 1,680
  • 18
  • 23