Search code examples
androidandroid-fragmentsandroid-pageradapter

Setting the item count in a PagerAdapter from the calling Activity


I have an Activity which calls a second Activity that contains a ViewPager.

The first activity includes Extra data that defines (indirectly) how many items there are in the ViewPager.

The calling code from the first Activity is:

                myIntent.setClass(getApplicationContext(),ShowAndTellMultiplesActivity.class);
                myIntent.putExtra("i_split_ways",v_integer_value);
                startActivity(myIntent);

The i_split_ways value is what is used to work out how many items to create in the ViewPager - essentially if this value is > 2 then the count should be i_split_ways -1 and if it is > 5 then the number of items should be 5.

In the receiving Activity I declare

static int i_split_ways = 2;

right after the Activity class declaration.

Then in the Activity's onCreate method is assign the value from the Extra data in the activity call as follows.

    Intent intent = getIntent();

    i_split_ways = intent.getExtras().getInt("i_split_ways",1);

Finally in the PagerAdapter class's getCount() function I calculate how many items I want to display as follows.

public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        if (i_split_ways > 5) i_split_ways = 5 ;
        if (i_split_ways <= 2) i_split_ways = 2;
        return i_split_ways;
    }

Note that the logic here requires a bit of work but that is not related to the problem here.

The problem I have is that when I launch the second activity when i_split_ways is anything other than 3 I get an error that says The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 2, found: 4 for example.

I can see that the problem probably relates to me needing to call

        mSectionsPagerAdapter.notifyDataSetChanged();

at some point, but I cannot figure out where I should call that - or indeed if there is something more fundamentally wrong with what I am trying to do.


Solution

  • You need to call mSectionsPagerAdapter.notifyDataSetChanged(); every time the underlying data changes.

    Judging by the information you've provided I'm guessing you're doing something like this:

    @Override
    protected void onCreate(Bundle savedInstance) {
        // calling super and setting content view
    
        mSectionsViewPager.setAdapter(mSectionsPagerAdapter);
    
        // maybe some more stuff
    
        Intent intent = getIntent();
        i_split_ways = intent.getExtras().getInt("i_split_ways",1);
    }
    

    If you would change the i_split_ways variable before setting the adapter to the viewpager you won't have to call notifyDataSetChanged(). So it would look something like this:

    @Override
    protected void onCreate(Bundle savedInstance) {
        // calling super and setting content view
    
        Intent intent = getIntent();
        i_split_ways = intent.getExtras().getInt("i_split_ways",1);
        mSectionsViewPager.setAdapter(mSectionsPagerAdapter);
    
        // maybe some more stuff 
    }