6

The problem: I have a ViewPager setup with a FragmentStatePagerAdapter that uses a global arraylist for the contents of its fragments. When I want to update this global array, I simply call the arraylist.add() method, instantiate a new FragmentStatePagerAdapter and a new ViewPager. However, for some reason, the Adapter isn't calling getItem at all. Not that it's data set is empty (it isn't) or anything... it simply isn't being called. I even set a log to test it. Here's my code:

// Create the adapter that will return a fragment for event
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getFragmentManager());

// Set up the ViewPager with the sections adapter.
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);

/**
 * A FragmentStatePagerAdapter that returns a fragment corresponding to
 * an index of the global events array
 */
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

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

    /**
     * Called to instantiate the fragment for the given page
     */
    @Override
    public Fragment getItem(int position) {
        Log.v("Miles", "getItem called");
        // getItem is called to instantiate the fragment for the given page.
        return EventFragment.newInstance(events.get(position));
    }

    /**
     * Total number of pages (fragments) there are
     * Given by size of the global events array minus 1
     */
    @Override
    public int getCount() {
        return events.size() - 1;
    }
}

Are there any problems I could have with this setup? I can verify that the global arraylist has data inside of it, but no fragment is being instantiated to reflect that.

Miles Peele
  • 325
  • 5
  • 15
  • 3
    You don't need to create a new adapter. Add the item to your list and call `notifyDataSetChanged` on the existing adapter. `getItem` is only called when the ViewPager needs the next page. It will not be called right away when you change the adapter data. – Karakuri Jul 25 '14 at 14:48
  • Right, so when I add the item to the arraylist, then go back to the activity holding the viewpager, nothing is displayed. As in, the item I just added to the arraylist is not there. I confirmed that there are no new fragments being added to the adapter by calling 'getCount()'. – Miles Peele Jul 25 '14 at 15:13
  • 1
    And you are calling `notifyDataSetChanged`? – Karakuri Jul 25 '14 at 15:23
  • I am, yeah. Sorry for the late response, was in a meeting. – Miles Peele Jul 25 '14 at 17:05
  • So I fixed the problem by making my adapter a global variable. I was hesitant to do this because I think it looks ugly, but it works. Thanks for your help. – Miles Peele Jul 25 '14 at 17:12

1 Answers1

0

You can try to override getItemPosition

override fun getItemPosition(object: Any): Int { return POSITION_NONE }

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33032720) – configbug Oct 31 '22 at 17:22