使用SherlockFragmentActivity和ListFragments时如何调用notifydatasetchanged

时间:2012-10-24 02:30:52

标签: android tabs android-listview actionbarsherlock

我将Sherlock Tab导航实现为以下两个类。班级Dashboard是主要活动。类DashboardContacts表示选项卡的一个片段。

我正在加载一段时间内加载的HTTP数据,当它加载时我需要DashboardContacts中的列表视图来反映从服务器带来的更改并将ListView从空白刷新到填充列表。为此,我通过notifyDataSetChanged中的刷新方法调用DashboardContacts方法,但在更改标签之前,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();
    }

1 个答案:

答案 0 :(得分:0)

我认为这是我没有实现的,所以我现在就做了并解决了这个问题:

public void setList(List<ExpensePool> list) {
    clear();
    addAll(list);
    notifyDataSetChanged();
}