0

I have been codeing a simple ExpandableListView with SQLite populating it, but stucked with an error, telling me, what i have a problem with a cursor. Here is my code

First of all - DBHelper. I have a tiny DB with two tables. First have a category name, wich can have unlimited subcategory titles.

public class DBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "main";
private static final int DB_VERSION = 1;

private SQLiteDatabase db;

public DBHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    this.db = this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(String.format("CREATE TABLE IF NOT EXISTS Category (CategoryName CHAR(50) PRIMARY KEY);"));
    db.execSQL(String.format("CREATE TABLE IF NOT EXISTS SubCategory (SubCategoryName CHAR(50) PRIMARY KEY, CategoryName CHAR(50), FOREIGN KEY (CategoryName) REFERENCES Category(CategoryName));"));
    db.execSQL(String.format("INSERT OR IGNORE INTO Category(CategoryName) VALUES('Games');"));
    db.execSQL(String.format("INSERT OR IGNORE INTO SubCategory(SubCategoryName, CategoryName) VALUES('Arcade','Games');"));

}

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

public Cursor fetchChildren(String cat) {
    String query = "SELECT * FROM SubCategory WHERE CategoryName = '" + cat + "'";
    return db.rawQuery(query, null);
}
}

MainActivity.java

public class MainActivity extends Activity {


SQLiteDatabase db;
DBHelper mDbh;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
Cursor mGroupsCursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mDbh = new DBHelper(this);
    mDbh.onOpen(db);
    //mDbh.onCreate(db);
    expListView = (ExpandableListView) findViewById(R.id.expandableListView);
    fillData();

    // preparing list data
    //prepareListData();

    listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

    // setting list adapter
    //expListView.setAdapter(listAdapter);

    // Listview on child click listener
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {
            final String str = listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition);
            Intent i = new Intent(com.example.senso.readernew.MainActivity.this,SingleListItem.class);
            i.putExtra("Name", str);
            startActivity(i);
            return false;
        }
    });
}


public class ExpandableListAdapter_DB extends SimpleCursorTreeAdapter {

    public ExpandableListAdapter_DB(Cursor cursor, Context context, int groupLayout,
                                    int childLayout, String[] groupFrom, int[] groupTo, String[] childrenFrom,
                                    int[] childrenTo) {
        super(context, cursor, groupLayout, groupFrom, groupTo,
                childLayout, childrenFrom, childrenTo);
    }


    @Override
    protected Cursor getChildrenCursor(Cursor groupCursor)
    {
        Cursor childCursor = mDbh.fetchChildren(groupCursor.getString(groupCursor.getColumnIndex("Category")));
        startManagingCursor(childCursor);
        childCursor.moveToFirst();
        return childCursor;
    }
}

private void fillData()
{
    mGroupsCursor = mDbh.fetchGroup();

    startManagingCursor(mGroupsCursor);
    mGroupsCursor.moveToFirst();
    expListView = (ExpandableListView)this.findViewById(R.id.expandableListView);

    ExpandableListAdapter_DB mAdapter = new ExpandableListAdapter_DB(mGroupsCursor,this,
            R.layout.list_group,
            R.layout.list_item,
            new String[]{"CategoryName"},
            new int[]{R.id.lblListHeader},
            new String[]{"SubCategoryName"},
            new int[]{R.id.lblListItem});

    expListView.setAdapter(mAdapter);
}
}

And, ofc, the error itself:

11-15 17:04:43.036 21404-21404/com.example.senso.readernew E/CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 1 columns. 11-15 17:04:43.046 21404-21404/com.example.senso.readernew E/AndroidRuntime: FATAL EXCEPTION: main 11-15 17:04:43.046 21404-21404/com.example.senso.readernew E/AndroidRuntime: Process: com.example.senso.readernew, PID: 21404 11-15 17:04:43.046 21404-21404/com.example.senso.readernew E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.senso.readernew/com.example.senso.readernew.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 11-15 17:04:43.046 21404-21404/com.example.senso.readernew E/AndroidRuntime: Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 11-15 17:04:43.046 21404-21404/com.example.senso.readernew E/AndroidRuntime: at com.example.senso.readernew.MainActivity.fillData(MainActivity.java:162) 11-15 17:04:43.046 21404-21404/com.example.senso.readernew E/AndroidRuntime: at com.example.senso.readernew.MainActivity.onCreate(MainActivity.java:46)

Thanks in advance!

P.S - i wrote my code, learning from Android ExpandableListView and SQLite Database one code.

Martin Pfeffer
  • 12,471
  • 9
  • 59
  • 68
Senso Hakai
  • 141
  • 1
  • 2
  • 13
  • your cursors dont have "CategoryName" / "SubCategoryName" columns, dump them with `DatabaseUtils#dumpCursor` – pskink Nov 15 '15 at 13:34
  • Couldn't read row 0, col -1 this causes the issue. I guess getColumnIndex("Category") returns "-1"?? – Martin Pfeffer Nov 15 '15 at 13:36
  • @MartinPfeffer, mGroupsCursor.getColumnIndex("Category") pop out -1, mGroupsCursor.getColumnIndex("CategoryName") pop out 0, so yeah – Senso Hakai Nov 15 '15 at 13:58
  • @pskink how can i do this? And where? I am kinda new for android, sorry – Senso Hakai Nov 15 '15 at 13:58
  • @pskink changed nothing, sadly. Also, i tried to dump cursor to string (not with dumping in fetches), and he tell me "">>>>> Dumping cursor android.database.sqlite.SQLiteCursor@21e362f0 { CategoryName=Games } <<<<< if it help you – Senso Hakai Nov 15 '15 at 14:23
  • @pskink uh actually i managed to do something like this. I'll posted mGroupsCursor = mDbh.fetchChildren(mGroupsCursor.getString(mGroupsCursor.getColumnIndex("CategoryName"))); Just after "move to first" in FillData(). Now it's blanc screen, but, at least, it something, and i overcomed the error. Howewer, this is not the way it mention to work - and i still have blanc screen. Blowing my head off – Senso Hakai Nov 15 '15 at 14:46
  • did you change `groupCursor.getColumnIndex("Category")` to `groupCursor.getColumnIndex("CategoryName")` inside `getChildrenCursor` method? – pskink Nov 15 '15 at 14:50
  • @pskink yeah, Is it right? – Senso Hakai Nov 15 '15 at 14:52
  • @pskink i have like `protected Cursor getChildrenCursor(Cursor groupCursor) { Cursor childCursor = mDbh.fetchChildren(groupCursor.getString(groupCursor.getColumnIndex("CategoryName"))); MainActivity.this.startManagingCursor(childCursor); childCursor.moveToFirst(); return childCursor; }` – Senso Hakai Nov 15 '15 at 14:54
  • so call `DatabaseUtils.dumpCursor` inside `fetchGroup` and `fetchChildren` – pskink Nov 15 '15 at 14:56
  • @pskink i pass them additional parametr (cursor) bcs there is no cursor type in DB. Or should i do something different? `public Cursor fetchGroup(Cursor cursor) { String query = "SELECT CategoryName FROM Category"; DatabaseUtils.dumpCursor(cursor); return db.rawQuery(query, null); } public Cursor fetchChildren(String cat, Cursor cursor) { String query = "SELECT * FROM SubCategory WHERE CategoryName = '" + cat + "'"; DatabaseUtils.dumpCursor(cursor); return db.rawQuery(query, null); }` – Senso Hakai Nov 15 '15 at 15:07
  • Also, the getChildrenCursor changes to `protected Cursor getChildrenCursor(Cursor groupCursor) { Cursor childCursor = mDbh.fetchChildren(groupCursor.getString(groupCursor.getColumnIndex("CategoryName")),groupCursor); MainActivity.this.startManagingCursor(childCursor); childCursor.moveToFirst(); return childCursor; }` – Senso Hakai Nov 15 '15 at 15:08
  • maybe, i'd better share my code? – Senso Hakai Nov 15 '15 at 15:12
  • so do that and dump your database (".dump" command in `sqlite3` tool) – pskink Nov 15 '15 at 15:14
  • @pskink returned to my first error, but know with `Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 2 columns` – Senso Hakai Nov 15 '15 at 15:33
  • @pskink https://dropmefiles.com/1G5ku - my project – Senso Hakai Nov 15 '15 at 15:58
  • no, sorry i will not download 6 MB, just share the source files, not resuorces (i have limited data plan) – pskink Nov 15 '15 at 16:00
  • @pskink sorry, here you go - https://dropmefiles.com/o1Pu6 – Senso Hakai Nov 15 '15 at 16:05
  • see this: http://codeshare.io/kByYh – pskink Nov 15 '15 at 17:01
  • @pskink holy cows, it worked. Now i create my app from this example.I am very, VERY grateful to you! – Senso Hakai Nov 15 '15 at 17:30

0 Answers0