1

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?

AlvaroSantisteban
  • 5,256
  • 4
  • 41
  • 62

4 Answers4

4
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.

JamesK
  • 164
  • 8
  • thanks for the explanation! Do you think that second approach is the right way to go? It seems to me a bit dirty :/ – AlvaroSantisteban Jun 11 '14 at 12:48
  • 1
    The alternative would be to create a new `ReportsListAdapter`, but you already have one. In this case I'd say clearing the list and adding new object data is the correct approach. As you mention, from API 11 you could have used `addAll(...)`, which would have tidied it up a bit. I assume you can't just add/remove/update based on differences between the current and new data. – JamesK Jun 11 '14 at 13:11
  • Thanks. I am going to leave the question open for a couple of days to see if anyone else comes with a different opinion, ok? :) – AlvaroSantisteban Jun 11 '14 at 13:28
1

First check if your method updateReports is really called after download. Second check if your reportsList did really change contents, you can simply log values of it before and after assigning with newReportsList.

ellexy
  • 66
  • 1
1
reportsList.clear();
reportsList.addAll(newReportsList);
listAdapter.notifyDataSetChanged();

This should work

g_vk
  • 106
  • 7
0

It is not the solution that I wanted, but until I find an explanation for my problem I will reset the adapter:

reportsList = newReportsList;
listView.setAdapter(listAdapter);

More info and approaches here: Android List view refresh

Community
  • 1
  • 1
AlvaroSantisteban
  • 5,256
  • 4
  • 41
  • 62