tags:

views:

1470

answers:

3

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 ???
 } 

}
+1  A: 

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.

CommonsWare
+2  A: 

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

Daniel Velkov
A: 

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

mPrinC