I’ve been wasting too much time on this one so have to ask again. I’ve no idea why this is happening at all.
I have an array adapter (aAdapter) and an array list (aList) and I’m trying to put a clear button to erase the entries in a database and to clear the list.
My problem is that NotifyDataSetChanged() just wont work from inside my onlick method here:
public void clearDB(View view) {
aList.clear();
aAdapter.notifyDataSetChanged();
HighScoresDB hsdb = new HighScoresDB(HighScoresActivity.this);
hsdb.openDB();
hsdb.clearDB();
hsdb.closeDB();
}
It works from everywhere else though. I’ve even tried putting the clear and notifyDataSetChanged() in another method and calling it but that doesn’t work either but did work when I called it from the onCreate….
Any ideas?
p.s. the database is being cleared.
Firstly I find the Android adapter implementation very flawed. When it comes to performing anything bespoke there seems to be ambiguous accounts of how to use it and the official documentation doesn’t clarify any of them. I would be very happy to be proved wrong with this.
The way I got consistent results when editing data in the view was as follows:
All changes to the underlying data structure being presented should be done in an AsyncTask which makes sense as you are changing things on the UI Thread and don’t want to have concurrency issues.
Operations on the underlying data structures should be performed by calling adapter methods so if you have a
ListAdapterthen you use theadd,removeandclearof the list adapter. This means the adapter manages view notifications etc. This generally leads to having to create a custom adapter as the methods available are limited (there isn’t even an add all in sdk versions before 7). You also end up with your adapter acting as a big fat controller, although I am aware we shouldn’t be viewing android as an MVC pattern it still seems wrong.I have created apps where I bypass adapter calls to operate on the underlying data structure and it has worked all through results ended up unpredictable unless you tightly managed notifications to the view. Now I just call through the adapter.
So although I am not able to explain why in
notifiyDataSetChangeddoesn’t work specifically in youronClickMethod. I am hopefully providing useful information which might help you to get your app working as expected.