0

follow Lazy download images into gridView I write this code to add more data after 5 row scroll.

MyGridViewAdater extend BaseAdapter seem normally. and in my page include gridview:

int current_page = 0;
@Override
    public void onCreate(Bundle savedInstanceState) {

     listDishEntity = dishDatabase.getAllDishs();   //421 size
            listDishDisplay = new ArrayList<DishEntity>();
            for (int i = 0; i < 15; i++) {
                listDishDisplay.add(listDishEntity.get(i));  // get 15 component
                                                           //first to iniate gridview

            }
}

// and display initiate data

DishImageGridViewAdapter adapter = new DishImageGridViewAdapter(
                    mContext, listDishDisplay, "");
            adapter.notifyDataSetChanged();
            gridNutri.setAdapter(adapter);



        gridNutri.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view,
                    int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                int lastInScreen = firstVisibleItem + visibleItemCount;
                // Toast.makeText(mContext, "lastInScreen: " + lastInScreen,
                // 1000).show();
                Log.d("lastInScreen", "" + lastInScreen);
        if ((lastInScreen == totalItemCount) && loadingMore) { //loadingMore iniate =false
                    // FETCH THE NEXT BATCH OF FEEDS
                    Log.d("totalItemCount", "" + totalItemCount);
                    new loadMorePhotos().execute();
                }
            }
        });

and here is asyTask class:

private class loadMorePhotos extends AsyncTask {

    @Override
    protected Void doInBackground(Void... arg0) {

        try {
            // SET LOADING MORE "TRUE"
            loadingMore = true;
            Log.d("loadingMore ", "" + loadingMore);
            // INCREMENT CURRENT PAGE
             current_page += 1;
            // Next page request

        } catch (Exception e) {
            Log.d("Exception ", "" + e.getMessage());
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        // SHOW THE BOTTOM PROGRESS BAR (SPINNER) WHILE LOADING MORE PHOTOS
        linlaProgressBar.setVisibility(View.VISIBLE);
        // Toast.makeText(mContext, "cac", 1000).show();
    }

    @Override
    protected void onPostExecute(Void result) {

        // get listview current position - used to maintain scroll position
        int currentPosition = gridNutri.getFirstVisiblePosition();

        // APPEND NEW DATA TO THE ARRAYLIST AND SET THE ADAPTER TO THE
        // LISTVIEW

        // listDishDisplay.clear();
        for (int i = ((current_page - 1) * tempDishUnitScroll); i < current_page
                * tempDishUnitScroll; i++) {
            listDishDisplay.add(listDishEntity.get(i));
        }
        Log.d("current_page ", "" + current_page);
        DishImageGridViewAdapter adapter = new DishImageGridViewAdapter(
                mContext, listDishDisplay, "");
        adapter.notifyDataSetChanged();
        gridNutri.setAdapter(adapter);

        // Setting new scroll position
        gridNutri.setSelection(currentPosition + 1);
        // SET LOADINGMORE "FALSE" AFTER ADDING NEW FEEDS TO THE EXISTING
        // LIST
        loadingMore = false;
        Log.d("loadingMore ", "" + loadingMore);
        linlaProgressBar.setVisibility(View.GONE);
    }

}

with each scroll to end, it will be show next 5 row ( 15 component). But it seem it only rune nomally for 4 times. See my log :

08-18 05:41:10.092: D/fapfap(1790): 9
08-18 05:41:10.112: D/onscroll loadingMore(1790): true
08-18 05:41:10.112: D/fapfap(1790): 12
08-18 05:41:10.236: D/onscroll loadingMore(1790): true
08-18 05:41:10.236: D/fapfap(1790): 12
08-18 05:41:10.248: D/onscroll loadingMore(1790): true
08-18 05:41:10.248: D/fapfap(1790): 12
08-18 05:41:10.264: D/onscroll loadingMore(1790): true
08-18 05:41:10.264: D/fapfap(1790): 12
08-18 05:41:10.276: D/onscroll loadingMore(1790): true
08-18 05:41:10.276: D/fapfap(1790): 12
08-18 05:41:10.296: D/onscroll loadingMore(1790): true
08-18 05:41:10.296: D/fapfap(1790): 12
08-18 05:41:10.316: D/onscroll loadingMore(1790): true
08-18 05:41:10.316: D/fapfap(1790): 12
08-18 05:41:10.336: D/onscroll loadingMore(1790): true
08-18 05:41:10.336: D/fapfap(1790): 15
08-18 05:41:10.336: D/totalItemCount(1790): 15
08-18 05:41:10.344: D/onscroll loadingMore(1790): true
08-18 05:41:10.344: D/loadingMore(1790): true
08-18 05:41:10.344: D/fapfap(1790): 15
08-18 05:41:10.344: D/totalItemCount(1790): 15
08-18 05:41:10.348: D/loadingMore(1790): true
08-18 05:41:10.456: D/onscroll loadingMore(1790): true
08-18 05:41:10.456: D/fapfap(1790): 15
08-18 05:41:10.456: D/totalItemCount(1790): 15
08-18 05:41:10.460: D/loadingMore(1790): true


08-18 05:41:10.468: D/current_page(1790): 2     ///// SO WHY 
08-18 05:41:10.468: D/loadingMore(1790): false  /////  current_page
08-18 05:41:10.468: D/current_page(1790): 3     /////    increase continuesly?
08-18 05:41:10.468: D/loadingMore(1790): false  ///// I do not know why @@
08-18 05:41:10.468: D/current_page(1790): 4     /////



08-18 05:41:10.468: D/loadingMore(1790): false
08-18 05:41:10.480: D/onscroll loadingMore(1790): false

and Max of data size is 60, not 421 :(

08-18 06:02:04.476: D/onscroll loadingMore(1790): false
08-18 06:02:04.476: D/fapfap(1790): 60
08-18 06:02:04.480: D/onscroll loadingMore(1790): false
08-18 06:02:04.480: D/fapfap(1790): 60
08-18 06:02:04.480: D/onscroll loadingMore(1790): false
08-18 06:02:04.480: D/fapfap(1790): 60
08-18 06:02:04.484: D/onscroll loadingMore(1790): false
08-18 06:02:04.484: D/fapfap(1790): 60
08-18 06:02:04.568: D/onscroll loadingMore(1790): false
08-18 06:02:04.572: D/fapfap(1790): 60
Community
  • 1
  • 1
kemdo
  • 1,349
  • 3
  • 14
  • 29
  • explain this `with each scroll to end, it will be show next 5 row ( 15 component). But it seem it only rune nomally for 4 times` ? what exactly you mean by `row` and `component`? – mmlooloo Aug 18 '14 at 01:44
  • 1 row with 3 component ( component means Image that display in girdView) So 5 row is 15 Image all – kemdo Aug 18 '14 at 04:40
  • so i think you have just 60 images yes? and `it only rune nomally for 4 times` so means : 4*15 = 60 and it is correct, isnt it? – mmlooloo Aug 18 '14 at 05:52
  • I have all 431 Image :D. sory for late answer , I am trying to solve this problem – kemdo Aug 18 '14 at 08:18

0 Answers0