2016-03-09 53 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創建後的值 –