【问题标题】:Not able to show all data fetch from sqlite db into Expandable list view无法将从 sqlite db 获取的所有数据显示到可扩展列表视图中
【发布时间】:2015-04-18 09:39:27
【问题描述】:

我正在将 .csv 文件中的数据插入 sqlite 数据库。但是当我从 sqlite 获取数据并将其显示到可扩展列表视图中时,它会跳过一些行数据并显示在可扩展列表视图中,即从 sqlite 显示到可扩展列表视图时缺少一些数据

主要活动

   ` databaseHelper = new DatabaseHelper(this);
    listDataHeader = new ArrayList<String>();
    childDataHashMap = new HashMap<String, List<ChildInfo>>();
    // get the listview
    expListView = (ExpandableListView)findViewById(R.id.expandableListView);
    //Group data//
    Cursor cursor = databaseHelper.getExpandableData();
    cursor.moveToFirst();
    Log.i(TAG, "cursor.getCount()" + cursor.getCount());
    do {
        Log.d(TAG,"Category "+ cursor.getString(cursor  .getColumnIndex("categorydesc")));
        String categoryDescription = cursor.getString(cursor    .getColumnIndex("categorydesc"));
        int categoryId = cursor.getInt(cursor.getColumnIndex("CategoryId"));
        Log.i(TAG, "categoryDescription:" + categoryDescription);
        listDataHeader.add(categoryDescription);

        //Child data//
        Cursor cursorChild = databaseHelper.fetchChildren(categoryId);
        List<ChildInfo> childList = new ArrayList<ChildInfo>();
        cursorChild.moveToFirst();
        while (cursorChild.moveToNext()) {
            String businessName = cursorChild.getString(cursorChild .getColumnIndex("BusinessName"));
            phoneNumber = cursorChild.getString(cursorChild.getColumnIndex("ph_Phone"));
            String landMark = cursorChild.getString(cursorChild.getColumnIndex("LandMark"));
            Log.w("", "Category Child " + businessName);
            ChildInfo childInfo = new ChildInfo(businessName, phoneNumber,landMark);
            childList.add(childInfo);
        }
        childDataHashMap.put(categoryDescription, childList);
    } while (cursor.moveToNext());
    cursor.close(); 
    listAdapter = new ExpandableListAdapterNew(this, listDataHeader,childDataHashMap);
    // setting list adapter
    expListView.setAdapter(listAdapter);

数据库助手

    public Cursor fetchGroup() {
        String query = "SELECT DISTINCT CategoryId, categorydesc FROM Category ";
        Cursor res = db.rawQuery(query, null);
        return res;
    }

    public Cursor getExpandableData() {
        String query = "SELECT * FROM Category GROUP BY categorydesc";
        return db.rawQuery(query, null);
    }

Logcat 输出

Number of Records(10600):  :: 11
MainAcitivity(10600): cursor.getCount()6
Category Hotels
         categoryDescription:Hotels
        Category Child GoodLuck
Category Restaurants
         categoryDescription:Restaurants
         Category Child ByThe Way
Category School
         categoryDescription:School
         Category Child Ornellas
Category Stationary
         categoryDescription:Stationary
         Category Child Venus
Category super shopee

实际类别为 6,但仅显示其中 5 个。

【问题讨论】:

  • 您能否格式化问题,以便我们在回答后阅读它。
  • 修复一些格式并删除一些杂乱无章

标签: android sqlite expandablelistview expandablelistadapter


【解决方案1】:

问题:

cursor.moveToFirst();       
Log.i(TAG, "cursor.getCount()"+cursor.getCount());
while (cursor.moveToNext()) // ...

您将光标移动到第一项,没有为此获取数据,移动到下一项。 对于子项 - 同样的问题。

【讨论】:

  • 我现在已经用 do..while 循环替换了它。它正在工作,但现在每个父级只加载 1 个子数据
  • @AkankshaSawarkar 请在第一篇文章中更新您的代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-17
  • 2018-02-16
  • 1970-01-01
  • 2019-02-10
  • 2016-05-21
  • 2018-02-19
相关资源
最近更新 更多