3

I have created an application in which i have loaded the data from sqlite by setting a limit of 10 and when the list gets scrolled down it load another 10 datas from sqlite using async task in this part i have fetched the header groups as well as child datas but i don't know how to put a child for the new dates can anyone tell me how to add a child to the particular parent in expandable listview

Async task:

 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;

    }

Part where i am confused:

 @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;
}
Ramesh R
  • 300
  • 4
  • 18
  • Can you post a snapshot of how the Data is stored in sqlite – avinash Jan 17 '17 at 05:25
  • You should add(one extra child load more type) as last child in each group of child list. and added condition in getChildView() method to check row is child type or load more type. – Chetan Joshi Jan 19 '17 at 07:59
  • Please check this previous reply: [**http://stackoverflow.com/a/15348971/2153132**](http://stackoverflow.com/a/15348971/2153132) – Apoorv Parmar Jan 19 '17 at 09:09

1 Answers1

0

You need to initialize the adapter of the listView inside the onCreate method:

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

After, in the background method, you need only to load the data and save in some class atribute. After, in the onPostResult method, you need to update the adapter and notify the 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;
}

This is the idea, change to your logic.

Alex Ferreira
  • 38
  • 1
  • 10