片段的onCreate()通过getArguments()显示不正确的索引值

时间:2014-03-25 13:50:05

标签: android fragment

我正在使用Fragments并有10项活动。我保存到newInstance()中的每个包中。但我在onCreate()中得到的错误或下一个索引值。我可以向左和向右滑动。但onCreate()显示下一个索引值。

我的代码: -

public static final ScorecardFragment newInstance(ScorecardHoles scorecardHoles, Context cnt) {

   context = cnt;

   holeNum = scorecardHoles.getHole();
   ScorecardFragment f = new ScorecardFragment();

    System.out.println("hole number in newInstance():-"+holeNum);

    Bundle bundle = new Bundle();
    bundle.putInt("holeNum", holeNum);
    f.setArguments(bundle);
    return f;
}

  @Override
public void onResume() {
   super.onResume();
   Bundle bundle = this.getArguments();
   holeNum = bundle.getInt("holeNum", 0);
   System.out.println("hole number in onResume():-"+holeNum);

}

我上一节打电话给Fragment。

    private void Initialize() {

    mViewPager = (ViewPager) findViewById(R.id.viewpager1);

    pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
    mViewPager.setAdapter(pageAdapter); 
    //mViewPager.setCurrentItem(2);
//  mViewPager.setOnPageChangeListener(new ScorecardFragment()); 
    pageAdapter.notifyDataSetChanged();

}

private List<Fragment> getFragments(ArrayList<ScorecardCourseDetails> list) {
     fList = new ArrayList<Fragment>();
        for(int i=0; i<18 ; i++) 
        {
         ScorecardFragment f1 = ScorecardFragment.newInstance(list.get(0).getScorecardHolesList().get(i), this);
         fList.add(f1);
        }
    return fList;

}

其他代码: -

public class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;

public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
    super(fm);
    this.fragments = fragments;
}

@Override
public Fragment getItem(int position) {
    return this.fragments.get(position);
}

@Override
public int getCount() {
    return this.fragments.size();
}

}

我的日志: -

03-25 19:48:20.135: I/System.out(12554): hole number in newInstance():-1
03-25 19:48:20.135: I/System.out(12554): hole number in newInstance():-2
03-25 19:48:20.135: I/System.out(12554): hole number in newInstance():-3
03-25 19:48:20.135: I/System.out(12554): hole number in newInstance():-4
03-25 19:48:20.135: I/System.out(12554): hole number in newInstance():-5
03-25 19:48:20.136: I/System.out(12554): hole number in newInstance():-6
03-25 19:48:20.136: I/System.out(12554): hole number in newInstance():-7
03-25 19:48:20.136: I/System.out(12554): hole number in newInstance():-8
03-25 19:48:20.136: I/System.out(12554): hole number in newInstance():-9
03-25 19:48:20.136: I/System.out(12554): hole number in newInstance():-10
03-25 19:48:20.136: I/System.out(12554): hole number in newInstance():-11
03-25 19:48:20.136: I/System.out(12554): hole number in newInstance():-12
03-25 19:48:20.136: I/System.out(12554): hole number in newInstance():-13
03-25 19:48:20.136: I/System.out(12554): hole number in newInstance():-14
03-25 19:48:20.136: I/System.out(12554): hole number in newInstance():-15
03-25 19:48:20.136: I/System.out(12554): hole number in newInstance():-16
03-25 19:48:20.136: I/System.out(12554): hole number in newInstance():-17
03-25 19:48:20.137: I/System.out(12554): hole number in newInstance():-18
03-25 19:48:20.191: I/System.out(12554): hole number in onResume():-1
03-25 19:48:20.191: I/System.out(12554): hole number in onResume():-2

1 个答案:

答案 0 :(得分:0)

  fList = new ArrayList<Fragment>();
        for(int i=0; i<18 ; i++) 
        {
         ScorecardFragment f1 = ScorecardFragment.newInstance(list.get(0).getScorecardHolesList().get(i), this);
         fList.add(f1);
        }

它应该是:

for (int i = 0; i < list.size(); i++) {
   ScorecardCourseDetails scorecardCourseDetails = list.get(i);
   for (int j = 0; j < scorecardCourseDetails.getScorecardHolesList().size(); j++) {
         ScorecardFragment f1 = ScorecardFragment.newInstance(scorecardCourseDetails.getScorecardHolesList().get(j), this);
         fList.add(f1);
   }

}