5

In my application I have to show the Student details in ViewPager. I used one fragment (say StudentPageFragment) and I write widget initializing code in onCreateView() like:

public static Fragment newInstance(Context context) {
    StudentPageFragment f = new StudentPageFragment();
    return f;
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.stud_list_page,
            null);
    // initialize all widgets here
    displayStudentDetails();
    return root;
}

protected void displayStudentDetails() {
    ArrayList<Student>studList = User.getStudentsList();
    if (studList != null) {
        int loc = (pageIndex * 3);
        for (int i = 0; i < 3; i++) {
            if (loc < studList.size()) {
                // populate data in view
            }
            loc++;
        }
    }
}

I have maintained a common ArrayList<Student> object which holds all the student objects.

And In displayStudentDetails() methods, I populate the first 3 Student objects there. If we swipe next page the same fragment should called the displayed next 3 Student objects.

And in ViewPagerAdapter class:

@Override
public Fragment getItem(int position) {
    Fragment f = new Fragment();
    f = StudentPageFragment.newInstance(_context);
    StudentPageFragment.setPageIndex(position);
    return f;
}

@Override
public int getCount() {
    return User.getPageCount();// this will give student list size divided by 3
}

Now my problem is all the pages holds first 3 student details. Please provide me the best way to do this.

Sridhar
  • 2,228
  • 10
  • 48
  • 79

1 Answers1

3

Now my problem is all the pages holds first 3 student details.

If this is happening, most likely your displayStudentDetails() method only gets the first three student details that you keep seeing and doesn't take in consideration the position(and the student details that come with that position) of the Fragment in the ViewPager. As you didn't posted the method I can't recommend a solution.

I have maintained a common ArrayList object which holds all the student objects.

Where did you do this and how do you store that list?

f = StudentPageFragment.newInstance(_context);

Please don't pass the Context to your fragments as the Fragment class has a reference to the Context/Activity through the getActivity() method that you should use instead.

You should build the Fragments like this:

@Override
public Fragment getItem(int position) {
    return StudentPageFragment.newInstance(position);
}

where the newInstance() method will be:

public static Fragment newInstance(int position) {
      StudentPageFragment f = new StudentPageFragment();
      Bundle args = new Bundle();
      args.putInt("position", position);
      f.setArguments(args); 
      return f;
}

You'll then retrieve the position and use it in the Fragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.stud_list_page,
            container, false);
    // initialize all widgets here        
    displayStudentDetails(getArguments().getInt("position"));
    return root;
}

In the displayStudentPosition you could get the values like this:

protected void displayStudentDetails(int position) {
        ArrayList<Student>studList = User.getStudentsList();
        if (studList != null) {
        for (int i = position; i < 3 && i < studList.size; i++) {
             // populate data
        } 
        } 
}
user
  • 86,916
  • 18
  • 197
  • 190
  • I have posted my displayStudentDetails() – Sridhar Mar 04 '13 at 05:58
  • In FragmentActivity, I retrieve Student list from server and parsed it and maintained in User class – Sridhar Mar 04 '13 at 05:59
  • @Sridhar As you get the student's data from the server on a background thread, the data isn't probably available when the `ViewPager` is shown to the user, right? – user Mar 04 '13 at 06:07
  • You are right, I set up the viewpager after I parsed the data from the server – Sridhar Mar 04 '13 at 06:10
  • @Sridhar Have you debugged the `displayStudentDetails` method and check what values do the `loc` and `pageIndex` have? Why not simply pass the `position` value that you pass to the `Fragment` in the adapter(which you should do through a Bundle anyway) and use that in the displaystudentdetails method? – user Mar 04 '13 at 06:27
  • i have passed the page index through adapter by setArgument method. How to get the argument in the Fragment class – Sridhar Mar 04 '13 at 06:33