Search code examples
androidandroid-listviewlistadapter

Replace all entries in list adapter


Can someone explain me why the following code does not work? I am trying to update my list of Report everytime the user clicks the refresh button with the following code

In updateReports (after all the download of data and the parsing):

reportsList = newReportsList;
listAdapter.notifyDataSetChanged();

Previously in onCreateView:

listAdapter = new ReportsListAdapter(activity, R.layout.list_item_reports, reportsList);
listView.setAdapter(listAdapter);

But the list does not refresh and keeps showing the old reports. I tried with listAdapter.clear() and listAdapter.add() with a for (I can not use addAll because I need to work with API 10) but it gave me a null pointer exception.

Any strategies to do this replacement in the cleanest way possible?


Solution

  • reportsList = newReportsList;
    listAdapter.notifyDataSetChanged();
    

    Does not work because you are just updating the reference reportsList holds with a reference to newReportsList. This does not update the reference the adaptor has (the original reference to the original array)

    Your second approach should work, but you'd need to post the class code and give detail of the NullPointerException.