Fill each fragment in ViewPager with another JSON object without reloading

i has ViewPager, which has Fragment, which contains the gridview in it,
my code works fine only when I scroll the page with a delay and each time I get the contents from the Internet again
all I want to do is just get the contents of each page once and use it. when the user views the page
One of my problems is that JSON is downloding simultaneouslywhen multiple pages scroll, and I don’t know how to save the contents of the gridview for next use, so that the page with the name


Is there a standard way to overcome this problem?


here's the PagerAdapter for my ViewPager:

    public class ViewPagerAdapter extends FragmentStatePagerAdapter {

    String[] tabName;

    public ViewPagerAdapter(FragmentManager fm, String[] tabArray) {
        super(fm);
        this.tabName = new String[tabArray.length]; 
        this.tabName = tabArray;
    }

    @Override
    public Fragment getItem(int position) {     

        TabPageFragment tabPageFragment = new TabPageFragment(position);
        return tabPageFragment;
    }

    @Override
    public int getCount() {
        return tabName.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabName[position];
    }
}

ViewPager :

public class TabPageFragment extends Fragment {

private GridView mgridview; // gridView inside Fragment
private int tabPosition;
private ArrayAdapterGridview arrayadapter; //adapter for my gridView

public TabPageFragment(int tabposition) {

    this.tabPosition = tabposition;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {             

    mgridview = (GridView) rootView.findViewById(R.id.gridView2);

    View rootView = inflater.inflate(R.layout.fragment_grid_view, container,false);

        DownloadContentTab dct = new DownloadContentTab();
        dct.execute();

    return rootView;
}

 // DownloadJSON AsyncTask
private class DownloadContentTab extends AsyncTask<Void, Void, Integer> {

    @Override
    protected Integer doInBackground(Void... params) {

        JSONfunctions jsf = new JSONfunctions(); //this function retrieve json for me
        JSONArray jsonarray = jsf.getJSONfromURL(myUrl); // this url is differ for each page

        String[] gridNameArray = new String[jsonarray.length()];
        String[] gridPicArray  = new String[jsonarray.length()];

        for (int j = 0; j < jsonarray.length(); j++) {

            try {
            JSONObject jsonobject = jsonarray.getJSONObject(j);                          
            gridNameArray[j] = jsonobject.getString("title");
            gridPicArray[j] = jsonobject.getString("pic");

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }   

        arrayadapter = new ArrayAdapterGridview(context, gridPicArray, gridNameArray);

        return 0;
    }

    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);

        mgridview.setAdapter(arrayadapter);

    }


   }    
 }
+4

All Articles