无法将所有数据从sqlite db中显示到可扩展列表视图中

时间:2015-04-18 09:39:27

标签: android sqlite expandablelistview expandablelistadapter

我将.csv文件中的数据插入到sqlite数据库中。但是当我从sqlite&amp ;;获取数据时将其显示在可扩展列表视图中,它会跳过某些行数据并显示在可扩展列表视图中,即从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个。

1 个答案:

答案 0 :(得分:0)

问题在于:

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

您将光标移动到第一个项目,没有获取此数据,移动到下一个。 对于儿童用品 - 同样的问题。