Using SimpleCursorTreeAdapter for ExpandableList gives error “Failed to read row 0, column -1”. Can't find the bug

I want to get data from a SQLite db and populate my ExpandableListView via a SimpleCursorTreeAdapter, but it fails somewehere. I really can't find what's wrong, don't even know where the exception is happening...


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,
            // TODO Auto-generated constructor stub

        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));

            return childrenCursor;

Here's the relevant part in my activity to set the ListAdapter:

private void fillData()
        Cursor dateCursor = dbAdapter.getAllDates();
        Log.d("fillData()", DatabaseUtils.dumpCursorToString(dateCursor));
        MyExpandableListAdapter myAdapter = new MyExpandableListAdapter(this, 
        new String[] {DbAdapter.KEY_VALUE_DATE},    
        new int[]{},             
        new String[] {DbAdapter.KEY_INDICATOR_NAME, DbAdapter.KEY_INDICATOR_UNIT, DbAdapter.KEY_VALUE_VALUE},
        new int[]{,, });

        Log.d("fillData()", "Creation successful");     
        Log.d("fillData()", "Adapter set");

Here are the two used functions of my 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);


        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);

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

And finally the output of LogCat:

09-07 12:05:06.912: D/fillData()(1317): >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@41219990
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(
09-07 12:05:07.145: E/AndroidRuntime(1317):     at android.database.AbstractWindowedCursor.getLong(
09-07 12:05:07.145: E/AndroidRuntime(1317):     at android.widget.CursorTreeAdapter$MyCursorHelper.getId(
09-07 12:05:07.145: E/AndroidRuntime(1317):     at android.widget.CursorTreeAdapter.getGroupId(
09-07 12:05:07.145: E/AndroidRuntime(1317):     at android.widget.ExpandableListConnector.getItemId(
09-07 12:05:07.145: E/AndroidRuntime(1317):     at android.widget.AbsListView.obtainView(
09-07 12:05:07.145: E/AndroidRuntime(1317):     at android.widget.ListView.measureHeightOfChildren(
09-07 12:05:07.145: E/AndroidRuntime(1317):     at android.widget.ListView.onMeasure(
09-07 12:05:07.145: E/AndroidRuntime(1317):     at android.view.View.measure(
09-07 12:05:07.145: E/AndroidRuntime(1317):     at android.widget.RelativeLayout.measureChild(
09-07 12:05:07.145: E/AndroidRuntime(1317):     at android.widget.RelativeLayout.onMeasure(
09-07 12:05:07.145: E/AndroidRuntime(1317):     at android.view.View.measure(
09-07 12:05:07.145: E/AndroidRuntime(1317):     at android.view.ViewGroup.measureChildWithMargins(
09-07 12:05:07.145: E/AndroidRuntime(1317):     at android.widget.FrameLayout.onMeasure(
09-07 12:05:07.145: E/AndroidRuntime(1317):     at android.view.View.measure(

As you can see, my last debug.log is that the Adapter is set to the ListView, but then it's finnito... I don't even get a log message that the overriden function getChildrenCursor(Cursor groupCursor) is ever called, so the error happens I don't know where...

Help, I'm going mad on this...


From the documentation of CursorTreeAdapter, which is an indirect parent of SimpleCursorTreeAdapter you can see that:

The Cursors must include a column named "_id" or this class will not work.

Which you were missing from your queries.

Need Your Help

PreferenceActivity error: doesn't show selected option after close Activity

android sharedpreferences android-preferences preferenceactivity

The value is saved well in SharedPreference when i push it , but it doesn't show when i open another time the PreferenceActivity. It runs if i don't put the android:entryValues , but i can't use it

Detecting Modified Files from Linux User Space

linux filesystems rsync

I need to generate a list of modified files since last list generation on a Linux system. I need to do this on user space, so FAM like solutions does not suit for me. To do this, I have some constr...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.