Question

I have dug deep down into SO but, although I have found other people asking similar questions to mine, I have not yet find a question that addresses the same issues I have. I have not found a satisfying answer either.

I have a ListView. When I call from the adapter, .notifyDataSetChanged, the ListView is updated, but I can see the update only once onResume() is called. In other words, I do not see it instantly, only after I leave the activity and comeback.

What can I do to see the update instantly? I have tried the .notifyDataSetChanged method, I have tried resetting the adapter... nothing worked.

Was it helpful?

Solution

According to your comment, you dont update the array IN the adapter, but an array held by the activity you passed to the adapter once. Thats why the adapter isnt updating properly. You are changing the array outside of your adapter-class, which might not be the same array-object your adapter is using. At onResume(), your adapter is recreated with the new array and showing the new content.

A solution would be using the following custom Adapter class:

class MyAdapter extends BaseAdapter {

    private Array[] myArray;

    public MyAdapter(Array[] myArray) {
        this.myArray = myArray;
    }

    public updateContent(Array[] myNewArray) {
        this.myArray = myNewArray;
        this.notifyDataSetChanged();
    }

    // your getItem, getView, and so on methods 
}

Then from your activity, simple call myArray.updateContent() with your new Array and it will update immediatly.


Its never good to hold and manipulate an object used from one class (the adapter) within another one (the activity). Try to move all code for manipulating the array into the adapter and use methods to add/remove items. This will make it a lot easier finding this kind of errors!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top