Joe
Joe

Reputation: 1498

notifyDataSetChanged() not updating list view when adding data

I am reading and adding data in ListView from 3 different xml feeds, the fetching process runs thrice for execute three different asynctasks.

Here is my code for onPostExecute,

if (fetchedData.isEmpty())
    fetchedData = result;
else 
    fetchedData.addAll(result);

if (sAdapter == null) {
    sAdapter = new SpecialAdapter(myapp.this, fetchedData,android.R.layout.simple_list_item_2, keys, ids);
    setListAdapter(sAdapter);
} else {
    sAdapter.notifyDataSetChanged();
}

in the above code fetchedData is an ArrayList.

The above code works fine and update the listView for all 3 feeds. But once the data of all the three feeds is added in the listView, I want to add another ArrayList data in my ListView.

Which means I want to add the data if all feeds are loaded so I added if block,

if (//all feeds added in the list) {
    Log.e("added","feeds added");
    // add another ArrayList in my ListView here 
    // and update the List adapter
}

and this is actual code for the above if block,

if (count == feeds.length) {

    Log.e("added","feeds added");
    //adding another arrayList in list view ArrayList
    fetchedData.addAll(test.secondryArray);
    // checking if it is added  (it is added successfully
    Log.e("size of fetchedData",fetchedData.size + "");
    // update the adapter
    sAdapter.notifyDataSetChanged();
}

In if block, I added a new arraylist in my list view array list and updated the SimpleAdapter, but the data is not displaying in the ListView for some reason. I first thought that the data is not added in the fetchedData array from test.secondaryArray but that is not true because data is added successfully in fetchedData array but the adapter is not being updated.

here is my complete inPostExecute code,

if (fetchedData.isEmpty())
    fetchedData = result;
else 
    fetchedData.addAll(result);

String[] keys = { GetFeeds.KEY_TITLE, GetFeeds.KEY_DATE};
int[] ids = { android.R.id.text1, android.R.id.text2 };

// KEY_TITLE = text1
// KEY_DATE  = text2

if (sAdapter == null) {
    sAdapter = new SpecialAdapter(myapp.this, fetchedData,android.R.layout.simple_list_item_2, keys, ids);
    setListAdapter(sAdapter);
} else {
    sAdapter.notifyDataSetChanged();
}

if (count == feeds.length) {
    Log.e("added","feeds added");
    //adding another arrayList in list view ArrayList
    fetchedData.addAll(test.secondryArray);
    // checking if it is added  (it is added successfully
    Log.e("size of fetchedData",fetchedData.size + "");
    // update the adapter
    sAdapter.notifyDataSetChanged();
}

Why my lsitview is not being updated after I add new data after all feeds are loaded?

Edit: If I am doing the same thing outside the if block then it is working but not working inside if block, but this is not what I want because I want this code to execute only when all feeds are added not before that.

Upvotes: 0

Views: 917

Answers (2)

bhuvesh
bhuvesh

Reputation: 749

this will happen when there is no data in your ListView previously. If your ListView is empty and you are trying to execute notifyDataSetChanged() after adding data in it instead of initializing your adapter then your ListView will stay empty.

So instead of using notifyDataSetChanged() on your adapter, re-initialize it for your second array.

In your block,

if (count == feeds.length) {
    Log.e("added","feeds added");
    //adding another arrayList in list view ArrayList
    fetchedData.addAll(test.secondryArray);
    // checking if it is added  (it is added successfully
    Log.e("size of fetchedData",fetchedData.size + "");
    // update the adapter
    sAdapter.notifyDataSetChanged();
}

replace,

sAdapter.notifyDataSetChanged();

with,

sAdapter = new SpecialAdapter(myapp.this, fetchedData,android.R.layout.simple_list_item_2, keys, ids);
setListAdapter(sAdapter);

Upvotes: 0

Krish
Krish

Reputation: 3885

One possibility is like , the first time when you call this method you are trying to notify the listview by using the MainThread (UIThread) and in the second scenario (the if condition become true) you are using a background thread for notifying the listview.

Upvotes: 1

Related Questions