how to refresh view pager data?

829 Views Asked by At

When I got data from service I reload all data in array list and pass to adapter but adapter display my old data.I pass new data in adapter when I print array list in adapter.show me old data.when I load new data I clear all data base also.

I load data in view pager so I create newInstance.so below code I get data from service and Store in arraylist and pass to adapter

adapter = new PagerAdapter(activity.getSupportFragmentManager(),"TotalCount",firstCountArrayList,secondAcoountArrayList,totalCountArrayList);
        pager.setPageMargin(-400);
        pager.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        pager.setOffscreenPageLimit(firstCountArrayList.size());

        tabs.setViewPager(pager,selected);

In PagerAdapter I got old arraylist data

public class PagerAdapter extends FragmentPagerAdapter {

    String TotalCount = "";
    ArrayList<String> firstCountArrayList,secondAcoountArrayList,totalCountArrayList;

    public PagerAdapter(FragmentManager fm, String TotalCount, ArrayList<String> firstCountArrayList, ArrayList<String> secondAcoountArrayList, ArrayList<String> totalCountArrayList
                        ) {
        super(fm);
        //super(fm);
        this.TotalCount=TotalCount;


        this.firstCountArrayList=firstCountArrayList;
        this.secondAcoountArrayList=secondAcoountArrayList;   
this.totalCountArrayList=totalCountArrayList;

    }

    @Override
    public CharSequence getPageTitle(int position) {
        return firstCountArrayList.get(position);
    }

    @Override
    public int getCount() {
        return firstCountArrayList.size();
    }
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
    @Override
    public Fragment getItem(int position) {




        Log.e(TAG," PagerAdapter firstCountArrayList==>"+firstCountArrayList.toString());
        return NewPageInstance.newInstance(position,firstCountArrayList,secondAcoountArrayList,totalCountArrayList);

after all data pass to NewPageInstance and display on TextView like

   public static NewPageInstance newInstance(int position, String totalCount, ArrayList<String> firstCountArrayList, ArrayList<String> secondAcoountArrayList, ArrayList<String> totalCountArrayList) {

    totalCount=totalCount;
    firstCountArrayList=firstCountArrayList;
    secondAcoountArrayList=secondAcoountArrayList;
    totalCountArrayList=totalCountArrayList;
    }

and all data display textview

this is working only first time .not refresh view pager data.

1

There are 1 best solutions below

0
Vicky On

add below code in your view pager adapter:-

public int getItemPosition(Object object) {
return POSITION_NONE;

}

or view pager changed listener notify your view pager adapter:-

mPagerAdapter.notifyDataSetChanged();