android无尽的gridview逻辑错误?

时间:2014-08-17 23:03:29

标签: android gridview android-asynctask

关注Lazy download images into gridView我编写此代码以在5行滚动后添加更多数据。

MyGridViewAdater扩展BaseAdapter似乎正常。  在我的页面中包含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

            }
}

//并显示启动数据

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();
                }
            }
        });

这里是asyTask类:

私有类loadMorePhotos扩展了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);
    }

}

每个滚动结束,它将显示下一个5行(15个组件)。但它似乎只有4次符文正常。见我的日志:

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

和最大数据大小为60,而不是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

0 个答案:

没有答案