2016-10-11 8 views
1

私は現在の位置に応じて正しいデータを表示するネストしたフラグメントに渡したい配列を持っています。ただし、表示される最初の2ページは同じです。デバッグすると、ページ0がスキップされ、配列のpos 1のデータが最初の2ページにロードされることが示されているようです。viewpager最初の2ページ同じ

ページを数回スワイプして開始位置までスワイプした後は、正しく表示されますが、初期設定は正しく行われません。

これは外側の断片に埋め込ま私adapaterある:

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { 

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

    } 

    @Override 
    public ScreenSlidePageFragment getItem(int position) { 
     Bundle bundle = new Bundle(); 

     int logo=logos[position]; 
     String win=wins.get(position); 
     String draw=draws.get(position); 
     String loss=losses.get(position); 
     String team=teams.get(position); 
     String data=output.toString(); 

     Log.e("check pos outer", "" + position+" "+teams.get(position)); 

     return ScreenSlidePageFragment.newInstance(logo,win,draw,loss,team,data); 
    } 

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

、これは私のネストされた断片である。

public class ScreenSlidePageFragment extends Fragment { 
private static JSONArray data; 
private TableLayout tableLayout; 
private LayoutInflater inflate; 
private static String wins; 
private static String draws; 
private static String losses; 
private static String teams; 
private static int logo; 

public static ScreenSlidePageFragment newInstance(int logos,String win,String draw,String loss,String team,String output) { 
    ScreenSlidePageFragment fragmentFirst = new ScreenSlidePageFragment(); 
    Bundle bundle = new Bundle(); 
    bundle.putInt("logo", logos); 
    bundle.putString("wins", win); 
    bundle.putString("draws", draw); 
    bundle.putString("losses", loss); 
    bundle.putString("teams",team); 
    bundle.putString("data", output); 
    fragmentFirst.setArguments(bundle); 
    return fragmentFirst; 
} 


public ScreenSlidePageFragment() { 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    logo = getArguments().getInt("logo"); 
    wins= getArguments().getString("wins"); 
    draws= getArguments().getString("draws"); 
    losses= getArguments().getString("losses"); 
    teams= getArguments().getString("teams"); 
    String stringData= getArguments().getString("data"); 
    try { 
     data=new JSONArray(stringData); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_screen_slide_page, container, false); 
    tableLayout=(TableLayout)rootView.findViewById(R.id.squads_tables); 
    inflate=inflater; 
    fillTable(); 

    return rootView; 
} 

private void fillTable() { 
    if (teams.length() > 0) { 

     View tr = inflate.inflate(R.layout.squadrow, null,false); 
     ImageView teamLogo=(ImageView)tr.findViewById(R.id.imageView); 
     Drawable logoDraw= ContextCompat.getDrawable(getActivity(), logo); 
     teamLogo.setImageDrawable(logoDraw); 
     TextView teamname=(TextView)tr.findViewById(R.id.name); 
     teamname.setText(teams); 
     TextView record=(TextView)tr.findViewById(R.id.textView3); 
     record.setText("wins: " + wins + " draws: " + draws + " lost: " + losses); 
     tableLayout.addView(tr); 

     try { 
      int currPos=0; 
      boolean passed=false; 
      playerloop: 
      for(int j=0;j<data.length();j++,currPos++){ 
       JSONObject jsonobject = data.getJSONObject(j); 
       String currTeamName=jsonobject.getString("name"); 
       String playerName=jsonobject.getString("Name"); 
       String position=jsonobject.getString("position"); 
       int age=jsonobject.optInt("Age", 0); 
       int played=jsonobject.optInt("played", 0); 
       int goals=jsonobject.optInt("goals", 0); 
       if(!currTeamName.equals(teams) && passed){ 

        break playerloop; 
       } 
       else if(currTeamName.equals(teams) && !playerName.equals("null")){ 
        if(!passed){ 
         passed=true; 
        } 



        View tr_player = inflate.inflate(R.layout.player_row, null,false); 
        TextView currPlayer=(TextView)tr_player.findViewById(R.id.name_txt); 
        currPlayer.setText(playerName); 

        TextView currPosiotion=(TextView)tr_player.findViewById(R.id.textView2); 
        currPosiotion.setText(position); 

        TextView currAge=(TextView)tr_player.findViewById(R.id.agetxt); 
        currAge.setText("Age: "+age); 

        TextView currMatches=(TextView)tr_player.findViewById(R.id.matchesTxt); 
        currMatches.setText("Matches: "+played); 

        TextView currGoals=(TextView)tr_player.findViewById(R.id.textView4); 
        currGoals.setText("Goals: "+goals); 

        tableLayout.addView(tr_player); 
       } 

      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    } 




} 

}

アイブは、新しいインスタンスを返すように異なるアプローチを試みたとデータの束を渡しますが、それを修正するものはありません。

答えて

0
private static JSONArray data; 
private TableLayout tableLayout; 
private LayoutInflater inflate; 
private static String wins; 
private static String draws; 
private static String losses; 
private static String teams; 
private static int logo; 

フラグメントクラスのすべてのメンバーは静的です。これはおそらくあなたが望むものではないでしょう。これは、すべてのフラグメントインスタンスでそれらが共有されているためです。

初期化時に、ViewPagerは現在のページと次のページを作成します。メンバーが共有されているため、同じデータを使用する両方のページにつながる可能性があります。

So: 'static'キーワードを削除するだけです。なぜここにそれが必要なのか分かりません。

+0

よろしくお願いします。 – user3146965

関連する問題