1
我有一個Sherlock標籤導航實現爲以下兩個類。 Dashboard
是主要活動。類DashboardContacts
代表選項卡片段之一。如何調用notifydatasetchanged當使用SherlockFragmentActivity和ListFragments
我正在加載一段時間內加載的HTTP數據,當它加載時我需要DashboardContacts
中的列表視圖來反映從服務器引入的更改並將ListView
從空白刷新到填充列表。爲此,我通過DashboardContacts
中的刷新方法調用notifyDataSetChanged
方法,但在更改選項卡之前,沒有更改會在ListView
中執行。
public class Dashboard extends SherlockFragmentActivity {
private DashboardContacts contactsTab=new DashboardContacts();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab = getSupportActionBar().newTab();
tab.setText(getResources().getStringArray(R.array.dashboardTabs)[0]);
tab.setTabListener(new DashboardHome());
getSupportActionBar().addTab(tab);
ActionBar.Tab tab2 = getSupportActionBar().newTab();
tab2.setText(getResources().getStringArray(R.array.dashboardTabs)[1]);
tab2.setTabListener(contactsTab);
getSupportActionBar().addTab(tab2);
//CALLS ASYNCLOADER HERE TO LOAD HTTP DATA
}
private void httpSuccessMethod() {
//Does some work and then calls:
contactsTab.refresh(datasource.getAllContacts());
}
這裏是DashboardContacts客艙佈局:
public class DashboardContacts extends SherlockListFragment implements ActionBar.TabListener{
private UserListAdapter listAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
listAdapter=new UserListAdapter(container.getContext(), ApplicationState.getInstance(container.getContext()).getUserCache());
setListAdapter(listAdapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onTabSelected(Tab tab,android.support.v4.app.FragmentTransaction ft) {
ft.replace(android.R.id.content, this,"contacts");
ft.attach(this);
}
@Override
public void onTabUnselected(Tab tab,android.support.v4.app.FragmentTransaction ft) {
ft.detach(this);
}
@Override
public void onTabReselected(Tab tab,android.support.v4.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
public void refresh(List<User> list) {
if(this.listAdapter != null) {
this.listAdapter.setList(list);
this.listAdapter.notifyDataSetChanged();
}
}
在自定義列表適配器的更新方法的樣子,所以我實際上改變的是數據有太多:
public void refresh(List<User> list) {
if(this.listAdapter != null) {
this.listAdapter.setList(list);
this.listAdapter.notifyDataSetChanged();
}
您的適配器是否會看到新項目? – Luksprog
嗨@Luksprog,謝謝你的回覆。回答你的問題,我認爲它確實如此。我做了一些修改,並在我的問題主體中添加了第三個片段。這是刷新方法,現在也正在傳遞數據集,然後調用notifyDataSetMethod。我沒有看到任何改變/改進 – kishu27
感謝@Luksprog通過引導你指向的地方 – kishu27