4

i have a ViewPager that has a Fragment which contains a GridView into it
my code works fine only when i'm scrolling page with delay and each time retrieve's content's from internet again
all i want to do is just get each page content's once and use it when user scroll's a page
one of my issues is that JSON downloding simultaneously when several pages has been scrolled and i don't know how to store my gridview content's for next usage that a page called


is there a standard way to conquer this issue ?


here is 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];
    }
}

each page of ViewPager use's this class for populate Fragment :

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's 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);

    }


   }    
 }
Mahdi Javaheri
  • 1,080
  • 13
  • 25

0 Answers0