2016-03-09 45 views
0

我已在folling FragmentPagerAdapterFragmentPagerAdapter - 设定计数和标题异步

public class GameListPagerAdapter extends FragmentPagerAdapter implements IGetGameListTaskListener { 

GameList _games; 
public int int_items = 6 ; 

public GameListPagerAdapter(FragmentManager fm) { 
    super(fm); 

    //async task to get a list of games 
    GetGameListTask getGamesTask = new GetGameListTask(this); 
    getGamesTask.execute(); 
} 

@Override 
public Fragment getItem(int position) 
{ 
    Bundle bundle = new Bundle(); 
    bundle.putInt("GameId", position); 

    //get the generic matchlistfragment and tell it which game it is 
    MatchListFragment matchListFragment = new MatchListFragment(); 
    matchListFragment.setArguments(bundle); 

    return matchListFragment; // always return the same fragment, just give it a different game id 
} 

@Override 
public int getCount() { 
    return int_items; 
} 

@Override 
public CharSequence getPageTitle(int position) { 

    //NEED HELP - Need to get the tiltle here 
    //return _games.getGames().get(position-1).getName(); 

    switch (position) { 
     case 0: 
      return "One"; 
     case 1: 
      return "Two"; 
     case 2: 
      return "Three"; 
     case 3: 
      return "Four"; 
     case 4: 
      return "Five"; 
     case 5: 
      return "Six"; 
    } 
    return null; 
} 



@Override 
public void onComplete(GameList data) { 
    //get the data on onPostExecute of my "getGamesTask" 
    _games = data; 

    //NEED HELP - need to set the item count here, but it fails** 
    //int_items = _games.getGames().size(); 
} 

}

这里是我的IGetGameListTaskListener接口:

public interface IGetGameListTaskListener { 
    public void onComplete(GameList data); 
} 

我这里的问题是,我从api获取游戏列表,这是设置为异步任务GetGameListTask。然后,我想为该列表中返回的每个游戏创建一个选项卡。

一旦有了游戏onComplete(GameList data)我需要设置int_items,以及int_items

然而,由于(我的理解)的FragmentPagerAdapter的生命周期清单,这些值需要提供前期。

我将如何处理我的FragmentPagerAdapter的异步选项卡的这种实现?

+1

不要事先创建适配器对象。在onPostExecute()中或在获得结果后创建它。获得结果后,可以将它们作为数组或列表发送给适配器的构造函数并使用它们。 –

+0

嘿,为什么不执行这个GetGameListTask getGamesTask = new GetGameListTask(this);在调用GameListPagerAdapter之前?并且在DoInBackGround完成之后,你可以通过GamelIst并设置大小,这种方法是否有任何疑问? –

+0

@ankitaggarwal这只是将问题移到我的viewpager。我现在必须在viewpager中执行后台任务,然后设置viewPager.setAdapter –

回答

1

将您的异步任务执行移动到您要设置适配器的任何位置的活动或片段。

GetGameListTask getGamesTask = new GetGameListTask(this); 
getGamesTask.execute(); 

在的AsyncTask的onPostExecute(),做

@Override 
public void onPostExecute(){ 
    GameList games; 
    GameListPagerAdapter adapter = new GameListPagerAdapter(getFragmentManager(), games);  
    view_pager.setAdapter(adapter); 
} 

创建适配器这样

public class GameListPagerAdapter extends FragmentPagerAdapter { 

    GameList _games; 
    public int int_items = 6 ; 

    public GameListPagerAdapter(FragmentManager fm, GameList _games) { 
     super(fm); 
     this._games = _games; 
    } 

    @Override 
    public Fragment getItem(int position) 
    { 
     Bundle bundle = new Bundle(); 
     bundle.putInt("GameId", position); 
     MatchListFragment matchListFragment = new MatchListFragment(); 
     matchListFragment.setArguments(bundle); 

     return matchListFragment; // always return the same fragment, just give it a different game id 
    } 

    @Override 
    public int getCount() { 
     return int_items; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return _games.get(position); 
    } 
} 
+0

shot ...标记为答案。 –

0

,而不是返回

public int int_items = 6 ;

@Override public int getCount() { return int_items; }

你可能想获取然后返回的游戏数量。

+0

如何设置该值并非真正的问题。我所描述的问题是,我只得到FragmentPageAdapter创建后的值 –