Dennie
Dennie

Reputation: 2651

How can refresh ListView when tab in android

My application have three tabs with ListView display inside each one. How to write code to refresh ListView when click between tabs ?

@Override
public void onTabChanged(String tabId) {        
    if(tabId == "tab_1")
    {
        refresh ListView ???
    }   

}

Upvotes: 2

Views: 9152

Answers (3)

mPrinC
mPrinC

Reputation: 9411

Actually you should do two things: - you have to ask adapter to notify ListView of new data changes: notifyDataSetChanged() but that will make effect next time ListView requires a view to show (let say after scrolling list). Another problem is that ListView will not ask for number of rows in list, so if list became shorter, you will get null pointer exception :)

What i prefer to use is something like:

if(filterProvider.containsChildren(position)){
// go into the hierarchy
    filterProvider.navigateToChildren(position);
    adapter.notifyDataSetChanged();
    adapter.notifyDataSetInvalidated();
    listView.invalidateViews();
}else{
    int ID = (int)filterProvider.getIdFromPosition(position);
    activity.setResult(ID);
    activity.finish();
}

Code is a part of a list with multiple levels (with hierarchy)

Greetings, Sasha Rudan

Upvotes: 0

Daniel
Daniel

Reputation: 27639

If you want to refresh manually, you can call notifyDataSetChanged() on the Adapter.

Upvotes: 4

CommonsWare
CommonsWare

Reputation: 1007554

You don't refresh the ListView. You ensure the underlying Adapter has the latest data, and it will work with the ListView to display it. So, for example, if you are using a SimpleCursorAdapter, call requery() on the Cursor.

Upvotes: 3

Related Questions