AsyncTask完成时加载TabPageIndicator

时间:2013-03-02 15:55:09

标签: android android-asynctask viewpagerindicator

我使用ViewPagerIndicator库并修改了SampleTabsDefault.java版本。

我想要的是什么:

我希望通过AsyncTask在后​​台加载一些数据,并在任务完成时调用setAdapter

问题:

我收到错误:ViewPager尚未绑定。

代码:

public class SampleTabsDefault extends FragmentActivity {
    private static final String[] CONTENT = new String[] { "Recent", "Artists",
            "Albums", "Songs", "Playlists", "Genres" };
    FragmentPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_tabs);
        new LoadContentTask().execute();
    }

    private void loadContent() {
        // TODO Auto-generated method stub

        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
        indicator.setViewPager(pager);
    }

    private class LoadContentTask extends AsyncTask<Object, Object, Object> {

        @Override
        protected Object doInBackground(Object... arg) {
            adapter = new GoogleMusicAdapter(getSupportFragmentManager());
            return true;
        }

        protected void onPostExecute(Object result) {
            // process result
            loadContent();
        }
    }

    class GoogleMusicAdapter extends FragmentPagerAdapter {
        public GoogleMusicAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return CONTENT[position % CONTENT.length].toUpperCase();
        }

        @Override
        public int getCount() {
            return CONTENT.length;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

任务完成时调用setContentView。