Mdlc
Mdlc

Reputation: 7288

notifyDataSetChanged() not updating listview

I'm updating my listview from my background method:

 runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                BaseAdapter adapter = new SimpleAdapter(
                        AllProductsActivity.this, productsList,
                        R.layout.list_item, new String[] { TAG_PID,
                                TAG_NAME},
                        new int[] { R.id.pid, R.id.name });
                // updating listview
                if(getListView().getAdapter() == null){ //Adapter not set yet.
                     setListAdapter(adapter);
                     Log.v("ha", "bla");
                }
                else
                { //Already has an adapter
                    adapter.notifyDataSetChanged();
                }
            }
        });

This code is executed:
-on first load
-on scroll to load more content, where I call adapter.notifyDataSetChanged(), but without success.

How can I update my code so I can use notifydatasetchanged and add the new data to the current data.

My whole code: http://pastebin.com/cWmday3i Any Help is appreciated.

Upvotes: 2

Views: 4437

Answers (4)

Hradesh Kumar
Hradesh Kumar

Reputation: 1805

Try this it will work.

getListView().getAdapter().notifyDataSetChanged()

Upvotes: -1

Nilesh Patel
Nilesh Patel

Reputation: 137

Put below line out side runOnUiThread

 adapter.notifyDataSetChanged();

Upvotes: -1

Tenfour04
Tenfour04

Reputation: 93551

notifyDataSetChanged() won't do anything if you don't first change the data. In your else block, you need to transfer your JSON results to the existing adapter before calling notifyDataSetChanged().

Also, your code above the if block should be moved inside the if block, since it is unused outside of the if block. That ambiguity might have led to your oversight.

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157437

the way you wrote your code you should always call setListAdapter, because every time the runnable inside runOnUiThread is executed, you create a new Adapter, probably with a new data set. The only difference between notifyDataSetChanged and NotifyDataSetChanged is that the second one does not belong to the Android's API

Upvotes: 2

Related Questions