如何在可扩展的listview android中将孩子添加到特定的父母

Posted

技术标签:

【中文标题】如何在可扩展的listview android中将孩子添加到特定的父母【英文标题】:How to add a child to particular parent in expandable listview android 【发布时间】:2017-05-27 21:58:07 【问题描述】:

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

异步任务:

 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;

【问题讨论】:

您能否发布数据在 sqlite 中的存储方式的快照 您应该在每组子列表中添加(一个额外的子加载更多类型)作为最后一个子。并在 getChildView() 方法中添加条件来检查行是子类型还是加载更多类型。 请查看之前的回复:http://***.com/a/15348971/2153132 【参考方案1】:

需要在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;

就是这个意思,换你的逻辑吧。

【讨论】:

以上是关于如何在可扩展的listview android中将孩子添加到特定的父母的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Android 中将 JSONArray 转换为 ListView? [复制]

如何将可扩展的 ListView 放入 ScrollView

在可扩展的 QML 应用程序中访问列表视图索引

如何在 Android Studio 中将 ListView 更改为 RecyclerView?

如何在当前活动中将搜索结果保存在 ListView (Android) 中

如何在可扩展列表视图中绑定 Json 数据?