gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:13:11 25 4
gpt4 key购买 nike

我正在创建一个应用程序,其中有一个可扩展的 ListView ,可以从 sql lite 加载数据。起初我将选择查询的限制设置为 10,当列表到达末尾时我想从 sql lite 加载另外 10 个数据到 ListView 。我使用了列表滚动选项来查找 ListView 中的最后一项,但它没有按预期工作 谁能告诉我如何从 sql lite 获取数据并在列表结束时将它们加载到 ListView 中.

查询:

 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;

可扩展 ListView :

    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 加载数据的正确方法是正确的,还是有更好的方法来加载数据。

最佳答案

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

关于Android listview 滚动到底部以从 sqlite 加载更多数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41606803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com