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.
If you are accessing it from outside scope, you can get it from the ListView object and cast it so you can call notifyDataSetChanged(). Like so: ServerItemAdapter listAdapter = (ServerItemAdapter) listView. getAdapter(); listAdapter.
notifyDataSetChanged. Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
Suppose your ListView displays some data stored in an ArrayList . After you change the contents of the ArrayList , you need to tell the list that the source of the data had changed and it needs to redraw itself to show the new data. So, that is where notifyDatasetChanged() comes in.
You are instantiating a new adapter each time. What you have to do is put the line where you instantiate the adapter before the click listener, and in the click listener modify that adapter and call notifyDataSetChanged() on it. You of course have to add some setters to your adapter in order to modify the data.
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 ListAdapter
then you use the add
, remove
and clear
of 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 notifiyDataSetChanged
doesn't work specifically in your onClick
Method. I am hopefully providing useful information which might help you to get your app working as expected.
While not pretty, you can just reinitialize the adapter instead of notifying it, I have seen that sometimes its the only way to make it work.
So a way I handled a similar problem to this is to basically reinitialize the adapter like blindstuff said.
public class Example extends Activity{
CustomAdapter adapter;
ArrayList<ArrayList<String>> info = new ArrayList<ArrayList<String>>();
final ListView list = (ListView) findViewById(R.id.listView_custom);
adapter = new CustomAdapter(this, diceInfo.get(id));
list.setAdapter(adapter);
Then in the onclick Listener
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
info.get(id).add("1,0,0,true");
adapter = new CustomAdapter(Example.this, info.get(id));
list.setAdapter(adapter);
}
});
The example doesn't have everything initialized but it gets to the point. I just make a new adapter and set it to the list view that I have. Works well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With