This interface provides random read-write access to the result set returned by a database query.
The cursor begins at position -1 (before the first row). On moveToNext() it will point to the first row and return either true (next row exists) or false (already at end of rows). Other options include moveToPrevious(), moveToFirst() and moveToLast(). getColumnIndex (String columnName) will return the index of the column with the given name (columns are numbered beginning with 0). Use one of the get methods to retrieve the actual value (e.g. getString(int columnIndex), getInt(int columnIndex)), passing in the column index as an argument. getCount() returns the number of rows that are in the Cursor. close() should be called when you are finished interacting with the Cursor to prevent memory leaks leading to app crashes. The following code snippet iterates through all of the rows in the Cursor and prints out what was in the table:
int wordCol = cursor.getColumnIndex(ExampleContract.COLUMN_WORD); int defCol = cursor.getColumnIndex(ExampleContract.COLUMN_DEFINITION); while (cursor.moveToNext()) { String word = cursor.getString(wordCol); String definition = cursor.getString(defCol); Log.v("Cursor Example", word + " - " + definition); } cursor.close();This code could be run in the postExecute() method of an AsyncTask so it has access to the data from the Cursor.
ud851-Exercises-student\Lesson08-Quiz-Example\T08.03
