作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我创建了一个应用程序,在该应用程序中,我通过设置 10 的限制从 sqlite 加载了数据,当列表向下滚动时,它使用异步任务从 sqlite 加载了另外 10 个数据,在这部分我已经获取了标题组作为以及子数据,但我不知道如何为新日期放置一个 child ,谁能告诉我如何在可扩展 ListView 中将一个 child 添加到特定父对象
异步任务:
class LoadDataTask extends AsyncTask<String, Void, String> {
ArrayList<Daybook> daybooks1 = new ArrayList<Daybook>();
@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>();
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 11,20";
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+", "");
}
for (int i = 0; i < daybooks1.size(); i++) {
listDataHeader.add(disorderedlist);
groupdaybooklists = databaseHandler.getAllDaywisedaybookdetails(newdate);
for (int j = 0; j < groupdaybooklists.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();
chid.add(new Daybooklist(name, desc, type, usertype, amtin, amtout, extamt, mobno, datess, time));
}
listDataChild.put(listDataHeader.get(i), chid);
}
} while (cursor.moveToNext());
}
cursor.close();
db.close();
} catch (Exception e) {
e.printStackTrace();
}
return disorderedlist;
}
@Override
protected void onPostExecute(String disorderedlist) {
listAdapter.notifyDataSetChanged();
loadingMore = false;
}
我感到困惑的部分:
@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>();
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 11,20";
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+", "");
}
for (int i = 0; i < daybooks1.size(); i++) {
listDataHeader.add(disorderedlist);
groupdaybooklists = databaseHandler.getAllDaywisedaybookdetails(newdate);
for (int j = 0; j < groupdaybooklists.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();
chid.add(new Daybooklist(name, desc, type, usertype, amtin, amtout, extamt, mobno, datess, time));
}
listDataChild.put(listDataHeader.get(i), chid);
}
} while (cursor.moveToNext());
}
cursor.close();
db.close();
} catch (Exception e) {
e.printStackTrace();
}
return disorderedlist;
}
最佳答案
需要在onCreate
方法中初始化listView的adapter:
listView.setAdapter(adapter); // Pode ser com as listas vazias
之后,在后台方法中,你只需要加载数据并保存在一些类属性中。之后,在onPostResult方法中,需要更新adapter并通知listView:
protected void onPostExecute(String disorderedlist) {
adapter.addGroupList(groupList); // Need to implement the method in the adapter
adapter.addChildList(childList); // Need to implement the method in the adapter
adapter.notifyDataSetChanged();
loadingMore = false;
}
思路是这样的,换个逻辑。
关于android - 如何在可扩展 ListView android中将 child 添加到特定的 parent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41619192/
我是一名优秀的程序员,十分优秀!