GeekedOut
GeekedOut

Reputation: 17185

Android ListView not updating after a call to notifyDataSetChanged

I have an Activity which I start like this:

public class MyProblemsActivity extends ListActivity 
{
    String[] PROBLEMS = new String[] {"One", "Two", "Three" };
    ArrayAdapter adapter;       

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        adapter = new ArrayAdapter<String>(this,R.layout.my_problems, PROBLEMS);

        setListAdapter(adapter);

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

and that works totally fine.

The problem I run into is after a call to a remote server via a Asynch call, I do this:

    @Override
    protected void onPostExecute(String result) 
    {       
            PROBLEMS = new String[] {"Hello", "Bye", "Hmmmmmm" };
            adapter.notifyDataSetChanged();
        }

But the screen does not update. What am I doing wrong here and how can I get the screen to update with the new values?

Thanks!!

Upvotes: 2

Views: 6372

Answers (3)

OneWorld
OneWorld

Reputation: 17671

I read that you updated the list from an asynchronous callback. Same was my case. I went to run that in UI Thread and suddenly it worked as expected! (Usually you get an exception if you don't do that. Here it just doesn't do refresh the list.) For my opinion, the other answers are just work arounds.

So, alter your code like this:

@Override
protected void onPostExecute(String result) 
{                  

  runOnUiThread(new Runnable() {

    @Override
    public void run() {
            PROBLEMS = new String[] {"Hello", "Bye", "Hmmmmmm" };
            adapter.notifyDataSetChanged();
    }
});
};

Upvotes: 1

Rahul garg
Rahul garg

Reputation: 9362

what's happening is at

PROBLEMS = new String[] {"Hello", "Bye", "Hmmmmmm" };

the PROBLEMS is getting the reference to new string array object...Thus the old reference is remaining as it is(unchanged)..

to correct it, use following :

PROBLEMS.clear();
List<String> newlist = new ArrayList<String>();
newlist.add(..);
..
PROBLEMS.addAll(newlist);
adapter.notifyDataSetChanged();

this way new string(s) will be added only to existing array reference pointed out by PROBLEMS

NOTE: i have mentioned by referencing the use of arraylist of string instead of string[] , to use clear(),addAll() functionalites of it, you can modify it for String[] as per your use..

Upvotes: 9

louielouie
louielouie

Reputation: 14941

If you want a simpler answer, you can just change your code to look like this:

@Override
protected void onPostExecute(String result) 
{       
    PROBLEMS = new String[] {"Hello", "Bye", "Hmmmmmm" };
    adapter.clear();
    adapter.addAll(PROBLEMS);
    adapter.notifyDataSetChanged();
}

This changes the ArrayAdapter's contents to have the Strings in your new String[], then notifies the ListView that the ArrayAdapter has new contents. This will cause the ListView to update and show your new Strings.

Upvotes: 1

Related Questions