如何在expandable listview android中将子项添加到特定父项

时间:2017-01-12 17:15:06

标签: android sqlite listview expandablelistview

我创建了一个应用程序,其中我通过设置限制为10从sqlite加载数据,当列表向下滚动时,它使用异步任务从sqlite加载另外10个数据,在此部分我已将标题组作为以及儿童数据,但我不知道如何为新日期安排孩子,任何人都可以告诉我如何在可扩展列表视图中将孩子添加到特定父母

异步任务:

public void WriteError(Exception ex)
{
    var doc = new XmlDocument();
    doc.Load(ex);
    sql.Insert(doc);
}

我很困惑的部分:

 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;

    }

1 个答案:

答案 0 :(得分:0)

您需要在onCreate方法中初始化listView的适配器:

listView.setAdapter(adapter); // Pode ser com as listas vazias

之后,在后台方法中,您只需要加载数据并保存在某个类属性中。之后,在onPostResult方法中,您需要更新适配器并通知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;
}

这是一个想法,改变你的逻辑。