Android listview滚动到底部以从sqlite加载更多数据

时间:2017-01-12 06:48:51

标签: android sqlite listview

我正在创建一个应用程序,其中我有一个可扩展的列表视图,它从sq lite加载数据。首先,我将限制设置为选择查询的10,当列表到达结尾时,我想从sq lite加载另外10个数据到列表视图。我已经使用list-scrolling选项查找列表视图中的最后一项,但它没有按预期工作,任何人都可以告诉我如何从sq lite获取数据并将其加载到列表中列表到达时查看。

查询:

 ArrayList<Daybook> daybookDetails = new ArrayList<Daybook>();
    String selectquery = "SELECT date,IFNULL(SUM(amountin),0) as amountin,IFNULL(SUM(amountout),0) as amountout,daybookusertype FROM daybookdetails GROUP BY strftime('%Y-%m-%d',date) ORDER BY strftime('%Y-%m-%d',date) DESC LIMIT " + s + "";
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectquery, null);
    if (cursor.moveToFirst()) {
        do {
            Daybook daybookentries = new Daybook();
            daybookentries.setDate(cursor.getString(0));
            daybookentries.setCashin(cursor.getString(1));
            daybookentries.setCashout(cursor.getString(2));
            daybookDetails.add(daybookentries);


        } while (cursor.moveToNext());

    }
    cursor.close();
    db.close();
    return daybookDetails;

可扩展列表视图:

    daybooks = new ArrayList<Daybook>();
    daybooks = databaseHandler.getAlldaybookentriesdatewise(olimit);
    daybooklists = new ArrayList<Daybooklist>();


    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<Daybooklist>>();

    for (int i = 0; i < daybooks.size(); i++) {
        String date = daybooks.get(i).getDate();
        if (date != null) {
            String s = date;
            String[] spiliter = s.split("-");
            String year = spiliter[0];
            String month = spiliter[1];
            String dates = spiliter[2];
            if (month.startsWith("01")) {
                disorderedlist = dates + "Jan" + year;
                disorderedlist = disorderedlist.replaceAll("\\s+", "");
            } else if (month.startsWith("02")) {
                disorderedlist = dates + "Feb" + year;
                disorderedlist = disorderedlist.replaceAll("\\s+", "");
            } else if (month.startsWith("03")) {
                disorderedlist = dates + "Mar" + year;
                disorderedlist = disorderedlist.replaceAll("\\s+", "");
            } else if (month.startsWith("04")) {
                disorderedlist = dates + "Apr" + year;
                disorderedlist = disorderedlist.replaceAll("\\s+", "");
            } else if (month.startsWith("05")) {
                disorderedlist = dates + "May" + year;
                disorderedlist = disorderedlist.replaceAll("\\s+", "");
            } else if (month.startsWith("06")) {
                disorderedlist = dates + "Jun" + year;
                disorderedlist = disorderedlist.replaceAll("\\s+", "");
            } else if (month.startsWith("07")) {
                disorderedlist = dates + "Jul" + year;
                disorderedlist = disorderedlist.replaceAll("\\s+", "");
            } else if (month.startsWith("08")) {
                disorderedlist = dates + "Aug" + year;
                disorderedlist = disorderedlist.replaceAll("\\s+", "");
            } else if (month.startsWith("09")) {
                disorderedlist = dates + "Sep" + year;
                disorderedlist = disorderedlist.replaceAll("\\s+", "");
            } else if (month.startsWith("10")) {
                disorderedlist = dates + "Oct" + year;
                disorderedlist = disorderedlist.replaceAll("\\s+", "");
            } else if (month.startsWith("11")) {
                disorderedlist = dates + "Nov" + year;
                disorderedlist = disorderedlist.replaceAll("\\s+", "");
            } else if (month.startsWith("12")) {
                disorderedlist = dates + "Dec" + year;
                disorderedlist = disorderedlist.replaceAll("\\s+", "");
            }
            listDataHeader.add(disorderedlist);
        }

        chid = new ArrayList<Daybooklist>();
        daybooklists = databaseHandler.getAllDaywisedaybookdetails(date);
        for (int j = 0; j < daybooklists.size(); j++) {


            String name = daybooklists.get(j).getName();
            String desc = daybooklists.get(j).getDescription();
            String type = daybooklists.get(j).getType();
            String usertype = daybooklists.get(j).getUsertype();
            String amtin = daybooklists.get(j).getAmountin();
            String amtout = daybooklists.get(j).getAmountout();
            String extamt = daybooklists.get(j).getExtraamt();
            String mobno = daybooklists.get(j).getMobileno();
            String dates = daybooklists.get(j).getSdate();
            String time = daybooklists.get(j).getCtime();


            if (name != null) {
                chid.add(new Daybooklist(name, desc, type, usertype, amtin, amtout, extamt, mobno, dates, time));

            }

        }


        listDataChild.put(listDataHeader.get(i), chid);
    }

ListView滚动:

 expListView.setOnScrollListener(new AbsListView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            int btn_initPosY = fabaddnew.getScrollY();
            int li_initPosY = li_general.getScrollY();
            if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {

                listAdapter.isScrolling(true);

                fabaddnew.animate().cancel();
                fabaddnew.animate().translationYBy(150);

            } else {

                listAdapter.isScrolling(false);
                fabaddnew.animate().cancel();
                fabaddnew.animate().translationY(btn_initPosY);

            }
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            listAdapter.isScrolling(true);

            //what is the bottom item that is visible

            int lastInScreen = firstVisibleItem + visibleItemCount;

            //is the bottom item visible & not loading more already? Load more!
            if ((lastInScreen == totalItemCount) && !(loadingMore)) {
                if (olimit > totalcount) {

                } else {
                    olimit = olimit + 10;
                    new LoadDataTask().execute(String.valueOf(olimit));
                }

            }

        }
    });

从sqlite加载更多数据的后台任务:

class LoadDataTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... olimits) {
        String limits = null;

        loadingMore = true;
        try {
            limits = olimits[0];
            grouplistDataHeader = new ArrayList<String>();
            grouplistDataChild = new HashMap<String, List<Daybooklist>>();
            groupdaybooklists = new ArrayList<Daybooklist>();
            childdaybook = new ArrayList<Daybooklist>();
            databasehandler = new DatabaseHandler(getApplicationContext());
            loadeddate = new ArrayList<String>();
            String selectquery = "SELECT date,IFNULL(SUM(amountin),0) as amountin,IFNULL(SUM(amountout),0),daybookusertype as amountout FROM daybookdetails GROUP BY strftime('%Y-%m-%d',date) ORDER BY strftime('%Y-%m-%d',date) DESC LIMIT '" + limits + "'";
            SQLiteDatabase db = databasehandler.getReadableDatabase();
            Cursor cursor = db.rawQuery(selectquery, null);
            if (cursor.moveToFirst()) {
                do {
                    loadeddate.add(cursor.getString(0));
                    for (String s : loadeddate) {
                        newdate = s;
                    }
                    String[] spiliter = newdate.split("-");
                    String year = spiliter[0];
                    String month = spiliter[1];
                    String dates = spiliter[2];
                    if (month.startsWith("01")) {
                        disorderedlist = dates + "Jan" + year;
                        disorderedlist = disorderedlist.replaceAll("\\s+", "");
                    } else if (month.startsWith("02")) {
                        disorderedlist = dates + "Feb" + year;
                        disorderedlist = disorderedlist.replaceAll("\\s+", "");
                    } else if (month.startsWith("03")) {
                        disorderedlist = dates + "Mar" + year;
                        disorderedlist = disorderedlist.replaceAll("\\s+", "");
                    } else if (month.startsWith("04")) {
                        disorderedlist = dates + "Apr" + year;
                        disorderedlist = disorderedlist.replaceAll("\\s+", "");
                    } else if (month.startsWith("05")) {
                        disorderedlist = dates + "May" + year;
                        disorderedlist = disorderedlist.replaceAll("\\s+", "");
                    } else if (month.startsWith("06")) {
                        disorderedlist = dates + "Jun" + year;
                        disorderedlist = disorderedlist.replaceAll("\\s+", "");
                    } else if (month.startsWith("07")) {
                        disorderedlist = dates + "Jul" + year;
                        disorderedlist = disorderedlist.replaceAll("\\s+", "");
                    } else if (month.startsWith("08")) {
                        disorderedlist = dates + "Aug" + year;
                        disorderedlist = disorderedlist.replaceAll("\\s+", "");
                    } else if (month.startsWith("09")) {
                        disorderedlist = dates + "Sep" + year;
                        disorderedlist = disorderedlist.replaceAll("\\s+", "");
                    } else if (month.startsWith("10")) {
                        disorderedlist = dates + "Oct" + year;
                        disorderedlist = disorderedlist.replaceAll("\\s+", "");
                    } else if (month.startsWith("11")) {
                        disorderedlist = dates + "Nov" + year;
                        disorderedlist = disorderedlist.replaceAll("\\s+", "");
                    } else if (month.startsWith("12")) {
                        disorderedlist = dates + "Dec" + year;
                        disorderedlist = disorderedlist.replaceAll("\\s+", "");
                    }
                    grouplistDataHeader.add(disorderedlist);
                    groupdaybooklists = databaseHandler.getAllDaywisedaybookdetails(newdate);



                } while (cursor.moveToNext());


                /*for (int j = 0; j < groupdaybooklists.size(); j++) {

                }
                */
            }


            cursor.close();
            db.close();
            } catch (Exception e) {
            e.printStackTrace();
        }



        return null;
    }

    @Override
    protected void onPostExecute(String s) {
       // listAdapter.setVTransactionList(grouplistDataHeader);

        for(int j = 0 ;j<daybooklists.size();j++){
            String name = groupdaybooklists.get(j).getName();
            String desc = groupdaybooklists.get(j).getDescription();
            String type = groupdaybooklists.get(j).getType();
            String usertype = groupdaybooklists.get(j).getUsertype();
            String amtin = groupdaybooklists.get(j).getAmountin();
            String amtout = groupdaybooklists.get(j).getAmountout();
            String extamt = groupdaybooklists.get(j).getExtraamt();
            String mobno = groupdaybooklists.get(j).getMobileno();
            String datess = groupdaybooklists.get(j).getSdate();
            String time = groupdaybooklists.get(j).getCtime();
            childdaybook.add(new Daybooklist(name, desc, type, usertype, amtin, amtout, extamt, mobno, datess, time));

        }
        for(int i = 0;i<grouplistDataHeader.size();i++){
            grouplistDataChild.put(grouplistDataHeader.get(i), childdaybook);
        }


        listAdapter.setTransactionList(grouplistDataHeader, grouplistDataChild);

        for (int i = 0; i < listAdapter.getGroupCount(); i++) {
            expListView.expandGroup(i);
        }
        loadingMore = false;

    }
    @Override
    protected void onCancelled() {
        // Notify the loading more operation has finished
        loadingMore = false;
    }
}

我没有正确地获取下一组数据,任何人都可以告诉我这是从sqlite加载数据的正确方法还是有更好的方法来加载数据#s。

2 个答案:

答案 0 :(得分:1)

用户 android.support.v4.widget.SwipeRefreshLayout 参考此链接Implementing Swipe to Refresh

答案 1 :(得分:0)

我建议您将代码迁移到使用RecycleView,这不仅会让您的生活变得更轻松,而且您的应用程序性能也会有所提高,因为RecyclerViews具有更好的内存管理实现。 您甚至可以检查here如何检测是否需要加载更多项目