If we got this errors :
1 | Android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 |
What we want?
We want to create a new records and read it.
Problem?
Data is created but we can’t read the new saved records.
Solution? using “moveToFirst()”. Here is the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | public Cursor createNewRecord() { // Set the new records ID long insertId = database.insert(dbHelper.TABLE_TRACKER, null, values); // Inserting a new records mCursor = database.query(dbHelper.TABLE_TRACKER, allColumns, dbHelper.COLUMN_ID + " = " + insertId, null, null, null, null); mCursor.moveToFirst(); // This will read the NEW RECORD DATA // Mapping cursor data into newData UserDatabase newData = cursorToUser(mCursor); // Close cursor connection mCursor.close(); return newData; } /** * Mapping cursor into UserDatabase handler * @param cursor * @return */ private UserDatabase cursorToUser(Cursor cursor) { UserDatabase userDatabase = new UserDatabase(); userDatabase.setId(cursor.getLong(0)); userDatabase.setDay(cursor.getInt(1)); userDatabase.setIsExercise(cursor.getInt(2)); return userDatabase; } |