How to initialize a ViewPagerAdapter in a Fragment when MainActivity's OnCreate() is called?

3.5k views Asked by At

I got a Fragment with a ViewPager and I got the FragmentStatePagerAdapter, which is initialized inside the Fragment's OnCreateView() method. To set the data, I created the method setData(List<String> data) inside the Adapter:

public void setData(List<String> data) {
    this.data = data;
    notifyDataSetChanged();
}

In my Fragment with the ViewPager I got:

public void setData(List<String> data){
    adapter.setData(data);
    setCurrentItem(0);
}

Everything is working fine so far, but if I call in MainActivity's OnCreate():

fragment = (FragmentViewPager) getSupportFragmentManager().findFragmentByTag(TAG_VIEW_PAGER);
fragment.setData(myDataList);

it gives my an Attempt to invoke virtual method ...setData(...) on a null object reference error, because the adapter isn't initalized yet.

How can I fix this?

MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
    }

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    showViewPager();
    fragment = (FragmentViewPager) getSupportFragmentManager().findFragmentByTag(TAG_VIEW_PAGER);
    fragment.setData(myDataList);

}


private void showViewPager() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment fragment = new FragmentViewPager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment, TAG_VIEW_PAGER).commit();
    fragmentManager.executePendingTransactions();

}

FragmentViewPager:

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View layout = inflater.inflate(R.layout.fragment_view_pager, container, false);

    viewPager = (ViewPager) layout.findViewById(R.id.pager);

    adapter = new ViewPagerAdapter(getChildFragmentManager());
    viewPager.setAdapter(adapter);

    return layout;
}

public void setData(List<String> data){
    adapter.setData(data);
    setCurrentItem(0);
}



public void setCurrentItem(int position) {
    viewPager.setCurrentItem(position);
}

ViewPagerAdapter:

public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private List<String> data;

public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
}

public void setData(List<String> data) {
    this.data = data;
    notifyDataSetChanged();
}


@Override
public Fragment getItem(int i) {
    Fragment fragment = new FragmentContent();
    Bundle args = new Bundle();
    args.putString(FragmentViewPager.ARG_STRING, data.get(i));
    fragment.setArguments(args);
    return fragment;

}

@Override
public int getCount() {
    if (data != null) {
        return data.size();
    } else {
        return 0;
    }

}



@Override
public CharSequence getPageTitle(int position) {
    return data.get(position);
}

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}
}
1

There are 1 answers

0
imrhung On BEST ANSWER

I think in the fragment, you can declare a List variable and set data to this variable in you setData() method.

public void setData(List<String> data){
    this.data = data;
}

And then in onCreateView() you will set the data variable of the fragment to the adapter.

adapter.setData(this.data);

I did not test that, please give comment when you tried. :)