0

now, I have created two fragments, one with a GoogleMap and one listFragment. Each 2 seconds I get update from server with new information, stored in an array. My question is, how to force update my list fragment with the new info to show? The newInstance method doesn't work. Here is my code:

public class MyPagerAdapter extends FragmentPagerAdapter {
    private int NUM_ITEMS = 2;

    public MyPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }
    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    // Returns the fragment to display for that page
    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        switch (position) {
            case 0: // Fragment # 0 - This will show FirstFragment
                return MapFragment.newInstance("Map");
            case 1:
                return new MyListFragment();
            default:
                return null;
        }
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    // Returns the page title for the top indicator
    @Override
    public CharSequence getPageTitle(int position) {
        return "Page " + position;
    }

}

position 1 in part 2 means the ListFragment I want to update

ViewPager vpPager = (ViewPager) findViewById(R.id.vpPager);
    adapterViewPager = new MyPagerAdapter(getSupportFragmentManager());
    vpPager.setAdapter(adapterViewPager);
    vpPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        // This method will be invoked when a new page becomes selected.
        @Override
        public void onPageSelected(int position) {
            if(position==1){

            }
        }

        // This method will be invoked when the current page is scrolled
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            // Code goes here
        }

        // Called when the scroll state changes:
        // SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
        @Override
        public void onPageScrollStateChanged(int state) {
            // Code goes here
        }
    });
Lukas Anda
  • 716
  • 1
  • 9
  • 30

1 Answers1

0

Using this answer - get a reference on your fragment and call proper method (depends on your realization of getting data in your ListView in ListFragment) in your onPageSelected method.

Community
  • 1
  • 1
Divers
  • 9,531
  • 7
  • 45
  • 88
  • Well, the problem is u can't assign adapter to listview in static method so that's my problem. The NotifyDataSetChanged solved my update issue but it also updated the map and I don't want that – Lukas Anda May 17 '15 at 18:10
  • Why static method? Read a answer by my link - you will get a link on instance of your fragment. – Divers May 17 '15 at 19:15
  • @Traabefi add coe how you add data in your ListFragment and I will write kickoff example as well you don't understand my thought. – Divers May 17 '15 at 19:27
  • Well, i finally resolved my issue. i will post the answer tomorow but your approach is correct and only way to do it. – Lukas Anda May 17 '15 at 19:52