1

Here , I explain my problem. I install a ViewPager and FragmentPagerAdapter so you can slide between two page. Each fragment contains data ( TextView ) . When creating my ViewPager with the data passed as a parameter everything goes well. But if I want to change the data (ie the content of the fragment) it does not refresh

public class MaPagerAdapter extends FragmentPagerAdapter {

    private Meteo[]lameteo;


    public MaPagerAdapter(FragmentManager fm, Meteo[]lameteo) {
            super(fm);
            this.lameteo = lameteo;
            Log.i("aa", "ville maPagerAdapter"+lameteo[0].getLoc());
    }

    @Override
    public Fragment getItem(int position) {
        switch(position) {
        case 0: return page_droite.newInstance(lameteo[0]);
        case 1: return page_gauche.newInstance(lameteo);
        }
        return null;
    }
    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    public void setData(Meteo[] meteo){
        this.lameteo=meteo;
    }
    @Override
    public int getCount() {
            return 2;
    }

}

And my main

if(mPagerAdapter!=null){ //if pagerAdapter already initialized
        mPagerAdapter.setData(meteo);
        mPagerAdapter.notifyDataSetChanged();
        }else
        mPagerAdapter = new MaPagerAdapter(getSupportFragmentManager(), meteo);
        ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
        pager.setAdapter(mPagerAdapter);

Can you help me please ?

montauvergne
  • 175
  • 3
  • 9

1 Answers1

0

No, the pager adapter doesn't work that way. Android doesn't realize that you have that particular data in your fragments, so it wouldn't know how to update it.

You'll have to get references to your fragments inside your setData call, and then you'll have to write new methods inside your fragments that you can call to reset their data. Inside the fragments, you'll have to make sure that you update the fragment's views with the new data. But as for you adapter, you can do something like this:

public class MaPagerAdapter extends FragmentPagerAdapter {

    private Meteo[]lameteo;
    private FragmentManager fm;

    public MaPagerAdapter(FragmentManager fm, Meteo[]lameteo) {
        super(fm);
        this.fm = fm;
        this.lameteo = lameteo;
        Log.i("aa", "ville maPagerAdapter"+lameteo[0].getLoc());
    }

    public void setData(Meteo[] meteo){
        this.lameteo=meteo;

        page_droite droite = (page_droite) getFragment(0);
        droite.setData(meteo[0]);
        page_gauche gauche = (page_gauche) getFragment(1);
        gauche.setData(meteo);
    }

    private Fragment getFragment(int position) {
        String tag = "android:switcher:" + R.id.viewpager + ":" + getItemId(position);
        return fm.findFragmentByTag(tag);
    }

Note that there is no need to call notifyDataSetChanged in this case, since you still have the same fragment in your adapter. When you implement the setData functions in your fragments, you need to make all the changes then.

Bruce
  • 2,377
  • 1
  • 17
  • 11