0

I have a main activity with a ViewPager. I am trying to populate the ViewPager using an Adapter.

This is my Fragment Class.

public class ClassListFragment extends Fragment {

private List<item_timetable_class> _classList;

public ClassListFragment() {
}

public ClassListFragment SetClassList (List<item_timetable_class> classList) {
    ClassListFragment fragment = new ClassListFragment();
    fragment._classList = classList;
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // some code here

    //In this line I am getting _classList = Null        

      class_card_adapter adapter = new class_card_adapter(_classList, getContext());

    // some code here
    }

}

This is how I am calling the Fragment. Please note that Just after creating a new ClassListFragment Object, I am calling SetClassList so that the internal variable _classList is Set.

    // Set up the ViewPager with the sections adapter.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    for (int i = 0; i < weekday_list.size(); i++) {
        ClassListFragment newFragment = new ClassListFragment();
        newFragment.SetClassList(classCardList);
        mSectionsPagerAdapter.addFragment(newFragment, weekday_list.get(i));
        i++;
    }

    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

Unfortunately, the onCreateView method is being executed when the below line is being executed in the main activity.

mViewPager.setAdapter(mSectionsPagerAdapter);

And at that time, the List _classList is coming as null.

What could be the problem and How can I resolve the issue?

Bluemarble
  • 1,925
  • 4
  • 20
  • 35

3 Answers3

0

You are creating ClassListFragment:

ClassListFragment newFragment = new ClassListFragment();

And then do not keep reference to fragment returned from SetClassList:

newFragment.SetClassList(classCardList);

Modify SetClassList:

public ClassListFragment SetClassList (List<item_timetable_class> classLis)
{
    _classList = classList;
    return this;
}

And keep reference to the returned Fragment:

newFragment = newFragment.SetClassList(classCardList);
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
0

Please refer: Best practice for instantiating a new Android Fragment

(The correct way to do what you want is using Bundle and Parcelable objects into the Fragment arguments)


Anyway, you called two separate "constructors" here.

ClassListFragment newFragment = new ClassListFragment();
newFragment.SetClassList(classCardList);

public ClassListFragment SetClassList returns a new ClassListFragment, it does not set the current one.


Try this instead.

public static ClassListFragment newInstance(List<item_timetable_class> classList) {
    ClassListFragment fragment = new ClassListFragment();
    fragment._classList = classList;
    return fragment;
}

And

for (int i = 0; i < weekday_list.size(); i++) {
    mSectionsPagerAdapter.addFragment(
        ClassListFragment.newInstance(classCardList), 
        weekday_list.get(i));
    // i++; // You already increment this in the loop...
}
Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Your answer is more informative! thanks, but R. Zagórski's answer involves least amount of code changes. Thanks, you all are awesome! – Bluemarble Feb 27 '17 at 20:41
0

What I would do is this:

    public static ClassListFragment newInstance(final List<item_time_table_class> classList) {
ClassListFragment fragment = new ClassListFragment();
Bundle args = new Bundle()
args.putParcelable(key, classList);
fragment.setArguments(args);
return fragment;
}

ClassListFragment::onCreate() {
classList = getArguments().getParcelable(key);
}
Loren Rogers
  • 325
  • 3
  • 13