2012-09-07 64 views
1

我想從SQLite數據庫獲取數據並通過SimpleCursorTreeAdapter填充我的ExpandableListView,但它失敗了。我實在找不到什麼是錯的,甚至不知道哪裏有異常情況發生......使用SimpleCursorTreeAdapter for ExpandableList給出錯誤「無法讀取第0行,第-1列」。找不到bug

MyExpandableListAdapter:

private class MyExpandableListAdapter extends SimpleCursorTreeAdapter 
    {  

     public MyExpandableListAdapter(Context context, Cursor cursor, 
       int groupLayout, String[] groupFrom, int[] groupTo, 
       int childLayout, String[] childFrom, int[] childTo) { 
      super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childFrom, 
        childTo); 
      // TODO Auto-generated constructor stub 
     } 

     @Override 
     protected Cursor getChildrenCursor(Cursor groupCursor) { 
      Log.d("Calling getChildrenCursor()","OK"); 
      Cursor childrenCursor = dbAdapter.getTrackedValuesByDate(groupCursor.getString(groupCursor.getColumnIndex(DbAdapter.KEY_VALUE_DATE))); 
      Log.d("getChildrenCursor()", DatabaseUtils.dumpCursorToString(childrenCursor)); 
      startManagingCursor(childrenCursor); 

      return childrenCursor; 
     }  
    } 

下面是我的活動相關的部分設置ListAdapter:

private void fillData() 
    { 
     Cursor dateCursor = dbAdapter.getAllDates(); 
     startManagingCursor(dateCursor); 
     Log.d("fillData()", DatabaseUtils.dumpCursorToString(dateCursor)); 
     MyExpandableListAdapter myAdapter = new MyExpandableListAdapter(this, 
     dateCursor, 
     R.layout.expandablelist_header_date, 
     new String[] {DbAdapter.KEY_VALUE_DATE},  
     new int[]{R.id.expandablelist_header_date},    
     R.layout.listview_tracked_values, 
     new String[] {DbAdapter.KEY_INDICATOR_NAME, DbAdapter.KEY_INDICATOR_UNIT, DbAdapter.KEY_VALUE_VALUE}, 
     new int[]{R.id.list_trackedvalue_indicatorname, R.id.list_trackedvalue_unit, R.id.list_trackedvalue_value }); 

     Log.d("fillData()", "Creation successful");  
     this.expandableList.setAdapter(myAdapter); 
     Log.d("fillData()", "Adapter set"); 
    } 

這裏是我將對DBAdapter兩個常用的功能:

public Cursor getTrackedValuesByDate(String dateString) 
    { 
     Cursor cursor = this.db.rawQuery("select * from "+DB_INDICATORS_TABLE+" i,"+ DB_VALUES_TABLE+" v where i." 
     + KEY_INDICATOR_ROWID+"=v."+KEY_VALUE_INDICATOR_ID+" and v."+KEY_VALUE_DATE+"='"+dateString+"'", null); 

     if(cursor!=null) 
      cursor.moveToFirst(); 

     Log.d("getTrackedValuesByDate()", DatabaseUtils.dumpCursorToString(cursor)); 
     return cursor; 
    } 

    public Cursor getAllDates() 
    { 
     Cursor c = this.db.rawQuery("Select distinct "+KEY_VALUE_DATE+" from "+DB_VALUES_TABLE, null); 
     if(c!=null) 
      c.moveToFirst(); 

     Log.d("getAllDates()", DatabaseUtils.dumpCursorToString(c)); 
     return c; 
    } 

和F logcat中的inally輸出:

09-07 12:05:06.912: D/fillData()(1317): >>>>> Dumping cursor [email protected] 
09-07 12:05:06.912: D/fillData()(1317): 0 { 
09-07 12:05:06.912: D/fillData()(1317): trackDate=07.09.2012 
09-07 12:05:06.912: D/fillData()(1317): } 
09-07 12:05:06.912: D/fillData()(1317): 1 { 
09-07 12:05:06.912: D/fillData()(1317): trackDate=08.08.2012 
09-07 12:05:06.912: D/fillData()(1317): } 
09-07 12:05:06.912: D/fillData()(1317): <<<<< 
09-07 12:05:06.952: D/fillData()(1317): Creation successful 
09-07 12:05:06.952: D/fillData()(1317): Adapter set 
09-07 12:05:07.012: E/CursorWindow(1317): Failed to read row 0, column -1 from a CursorWindow which has 2 rows, 1 columns. 
09-07 12:05:07.012: D/AndroidRuntime(1317): Shutting down VM 
09-07 12:05:07.048: W/dalvikvm(1317): threadid=1: thread exiting with uncaught exception (group=0x40a13300) 
09-07 12:05:07.145: E/AndroidRuntime(1317): FATAL EXCEPTION: main 
09-07 12:05:07.145: E/AndroidRuntime(1317): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 
09-07 12:05:07.145: E/AndroidRuntime(1317):  at android.database.CursorWindow.nativeGetLong(Native Method) 
09-07 12:05:07.145: E/AndroidRuntime(1317):  at android.database.CursorWindow.getLong(CursorWindow.java:507) 
09-07 12:05:07.145: E/AndroidRuntime(1317):  at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:75) 
09-07 12:05:07.145: E/AndroidRuntime(1317):  at android.widget.CursorTreeAdapter$MyCursorHelper.getId(CursorTreeAdapter.java:436) 
09-07 12:05:07.145: E/AndroidRuntime(1317):  at android.widget.CursorTreeAdapter.getGroupId(CursorTreeAdapter.java:191) 
09-07 12:05:07.145: E/AndroidRuntime(1317):  at android.widget.ExpandableListConnector.getItemId(ExpandableListConnector.java:422) 
09-07 12:05:07.145: E/AndroidRuntime(1317):  at android.widget.AbsListView.obtainView(AbsListView.java:2288) 
09-07 12:05:07.145: E/AndroidRuntime(1317):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1244) 
09-07 12:05:07.145: E/AndroidRuntime(1317):  at android.widget.ListView.onMeasure(ListView.java:1156) 
09-07 12:05:07.145: E/AndroidRuntime(1317):  at android.view.View.measure(View.java:15172) 
09-07 12:05:07.145: E/AndroidRuntime(1317):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602) 
09-07 12:05:07.145: E/AndroidRuntime(1317):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415) 
09-07 12:05:07.145: E/AndroidRuntime(1317):  at android.view.View.measure(View.java:15172) 
09-07 12:05:07.145: E/AndroidRuntime(1317):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814) 
09-07 12:05:07.145: E/AndroidRuntime(1317):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
09-07 12:05:07.145: E/AndroidRuntime(1317):  at android.view.View.measure(View.java:15172) 

正如你看到的,我上次是的debug.log該適配器設置到ListView,但隨後 它finnito ...... 我甚至不得到日誌消息在被覆蓋的功能getChildrenCursor(光標groupCursor)是不斷調用,所以錯誤發生了,我不知道在哪裏...

幫助,我會在這個瘋狂...

回答

2

作品從CursorTreeAdapter的文件,這是SimpleCursorTreeAdapter的間接母公司你可以看到:

遊標必須包含名爲「_id」的列,否則該類將不起作用。

您從查詢中遺漏了哪些內容。

1

幾個百分點

您可能有空的Curso r返回(即不檢查c!= null嘗試檢查遊標列數或檢查c.moveToNext而不是檢查

還用try {} catch(SQLiteException)包裝所有rawQuery和Cursor方法

記錄此異常可能會揭示更多

+0

我照你的建議做了,但仍然一樣,所以查詢返回一些內容,並且沒有任何異常觸發它們:-( –

+0

列== -1意味着列不存在,你需要你可以檢查設置您的適配器時的值 – gheese

+0

但問題是列真的存在(我使用它們中的每一個的常量),這使得它很奇怪......並且不可能追溯錯誤: - / –

0

好的,我無法使它與SimpleTreeAdapter一起工作......根本找不到該錯誤。我現在所做的就是跳過TreeAdapter並將遊標的內容填充到集合中,然後通過BaseExpandableListAdapter填充列表。

它更復雜,而不是優雅的SimpleTreeAdapter,但至少現在;-)

相關問題