Array Adapter notifyDataSetChanged() will not work

14,755

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
14,755

Related videos on Youtube

A_Porcupine
Author by

A_Porcupine

Updated on June 04, 2022

Comments

  • A_Porcupine
    A_Porcupine almost 2 years

    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.

  • A_Porcupine
    A_Porcupine over 12 years
    Thanks for all the information I'll definitely implement some of your ideas. Sadly this is for a uni assignment and I'm running out of time as I'm going on holiday tomorrow so I think I'll try blindstuffs suggestion for now. However, I will be publishing this on to the market after I've handed this is so will try some of your ideas before then. :)
  • Fuzzical Logic
    Fuzzical Logic over 12 years
    I agree, whatsthebeef!! I don't even use specific "documented functionality" anymore because I can't guarantee that they work at all times (even given the documentation provided). In fact, I've run into porcupine's issue before and I don't even use notifydatachanged() anymore either. :( I just make my own event handlers. It's sad, really. This comment relates specifically to Android adapters, not the SDK as a whole
  • Aharon Manne
    Aharon Manne over 9 years
    This was also the only way I could get it to clean itself.
  • Sahil
    Sahil over 8 years
    I found reinitializing the adapter to be the only solution that worked! thanks
  • Paweł Poręba
    Paweł Poręba over 8 years
    Yeah, happend to me today, too. I've had like 3 methods based on results from other activities, and in one of them notifying the adapter wouldn't do anything.