In my application I have to show the Student details in ViewPager
. I used one fragment (say StudentPageFragment
) and I write widget initializing co
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
}
}
}