How get access to purpose instance of class?

The example code found on https://github.com/chrisbanes/cheesesquare. I create four instance of CheeseListFragment. I cannot understand how to gain access to the third instance of the class and pass the data? Particularly to the third

public class MainActivity extends AppCompatActivity implements CheeseListFragment.OnDataPass {
     Adapter adapter;

...
      adapter = new Adapter(getSupportFragmentManager());

            for (int i=0; i<4; i++){
                adapter.addFragment(new CheeseListFragment(), items[i]);
            }
...

 viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                onDataPass(String.valueOf(position)); // how send data to third instance of CheeseListFragment 





     static class Adapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();
        private final List<String> mFragmentTitles = new ArrayList<>();



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

        public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }

        @Override
        public Fragment getItem(int position) {


            return mFragments.get(position);
        }

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

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




public class CheeseListFragment extends Fragment {

    List<News> lnews;
    private RecyclerViewAdapter mRecyclerviewAdapter;
    OnDataPass dataPasser;

...
    protected String[] mDataset;



    public interface OnDataPass {
        public void onDataPass(String data);
    }

    @Override
    public void onAttach(Activity a) {
        super.onAttach(a);
        dataPasser = (OnDataPass) a;

    }
    public void passData(String data) 
        dataPasser.onDataPass(data);
    }


Source: oop

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.