0

I have a viewpager with an adapter that extends FragmentStatePagerAdapter, this has to show some fragment that always repeats the same layout (in whic there is a listView) for every page ... now the problem is how can I update the listView that is currently visible by clicking on a button that is outside of the Fragment? I can refresh only the ones that are in the not visible pages,but not the ones that are displayed or adjacent... One possible solution is to reset the adapter, but it doesn't seems to be right one, because it' s slow... could you please help me?

  public class MainActivity extends FragmentActivity implements OnClickListener{

    @Override
    protected void onCreate(Bundle arg0) {
            // TODO Auto-generated method stub
            super.onCreate(arg0);
            setContentView(R.layout.layout1);
             pagerAdapter = new AdapterPager(getSupportFragmentManager());
            ctx=this;
          mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(pagerAdapter);
            pagerTab=(PagerTabStrip) findViewById(R.id.pager_tab_strip);
           mViewPager.setCurrentItem(paginaCentrale);
            modifica=(ImageView)findViewById(R.id.modifica);
         modifica.setOnClickListener(this);


    }

    @Override
    public void onClick(View arg0) {
            // TODO onClick
            switch (arg0.getId()){
            case R.id.modifica:

                 //here I would like to call the method caricaLista() of the fragment

            break;
            }
    }


    public static class AdapterPager extends FragmentStatePagerAdapter {
   public AdapterPager(FragmentManager fm) {
            super(fm);
        }

        @Override
            public int getItemPosition(Object object) {
                    // TODO Auto-generated method stub

                    return super.getItemPosition(object);
            }


            @Override
        public Fragment getItem(int i) {
            Fragment fragment;
            Bundle args = new Bundle();
          args.putInt("position", i);
            fragment=new Fragment1();
          fragment.setArguments(args);
     return fragment;
        }

        @Override
        public int getCount() {
            return 1000;}

        @Override
        public CharSequence getPageTitle(int i) {
            String title=String.valueOf(i);

            return title;
        }


    }

 public static class Fragment1 extends Fragment implements OnItemClickListener{

int fragmentPosition;
ListView lv;
      List<Lista> lista= new ArrayList<Lista>();

      public Fragment1(){    
          }
          public static Fragment1 newInstance(){
            return new Fragment1();
          }

  @Override
            public void onResume() {
                    // TODO Auto-generated method stub
                    super.onResume();
                    caricaLista("")}
            }
    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle
savedInstanceState)    {
            View rootView = inflater.inflate(R.layout.fragment1, container, false);

lv=(ListView) rootView.findViewById(R.id.listViewCompiti);

            Bundle args = getArguments(); 
            int position=  args.getInt("position");

            fragmentPosition=position;
            lv.setOnItemClickListener(this);
     homeListAdapter= new HomeListAdapterWithCache(ctx,R.layout.list_item_home,lista);
                    lv.setAdapter(homeListAdapter);



    return rootView;
}
marcox1994
  • 43
  • 5

1 Answers1

0

You have to store the Current Fragment into your MainActivity , and change every time the page changes. So you call the method of the current fragments.

Check out how your MainActivity should look like:

public class MainActivity extends FragmentActivity implements OnClickListener{

    &Fragment mCurrentFragment;*

    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.layout1);
        pagerAdapter = new AdapterPager(getSupportFragmentManager());
        ctx=this;
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(pagerAdapter);
        pagerTab=(PagerTabStrip) findViewById(R.id.pager_tab_strip);
        mViewPager.setCurrentItem(paginaCentrale);
        modifica=(ImageView)findViewById(R.id.modifica);
        modifica.setOnClickListener(this);

        *mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
            mCurrentFragment = pagerAdapter.getItem(position);
            }*

    });


@Override
    public void onClick(View arg0) {
            // TODO onClick
        switch (arg0.getId()){
        case R.id.modifica:
            *mCurrentFragment.caricaLista();*
        break;
        }
}

}
Gabriel Molter
  • 333
  • 2
  • 10
  • really thank you for the answer... but when i click on it nothing seems to be updated it remains the same :( i can see from the logCat that the method has been called, but why the fragment doesn't change? – marcox1994 Jul 18 '13 at 19:06
  • what the method does ? its a list ? i guess you have to call notifyDataSetChanged on the adapter insider your funcion caricaLista – Gabriel Molter Jul 18 '13 at 20:43
  • the method by itself works, in fact the listView onStart is populated correctly... (i call notifyDataSetChanged... ) – marcox1994 Jul 19 '13 at 06:38
  • @marcox1994 edit your post with the code of _caricaLista()_ so i be able to help – Gabriel Molter Jul 19 '13 at 17:48
  • @marcox1994 you could be kind, and post your solution to we close this toppic. – Gabriel Molter Jul 20 '13 at 13:51
  • i created an HashMap where i could store all the fragment in the method "getItem" of the adapter... as a key i used the position of the fragment, so that if i wanted the current fragment i could take from the map – marcox1994 Jul 20 '13 at 15:07