What is use of Cursor in Android Development?
Cursor is the Interface which represents a 2 dimensional table of any database. When you try to retrieve some data using SELECT statement, then the database will first create a CURSOR object and return its reference to you.
The pointer of this returned reference is pointing to the 0th location which is otherwise called as before first location of the Cursor, so when you want to retrive data from the cursor, you have to first move to the first record so we have to use moveToFirst
When you invokes moveToFirst() method on the Cursor, it takes the cursor pointer to the first location. Now you can access the data present in the first record
In simple words, Cursor is a Interface whice returns collection of your query data. moveToFirst()
is used to point the cursor position from where you want to get data from your cursor. There are methods moveToLast()
, moveToNext()
, moveToPrevious()
, moveToPosition(position)
by which you can iterate through your cursor by desired way.
For example, you have data in your Cursor
Lalit
Rithesh
Paresh
Chandra
moveToFirst()
- If you usecursor.moveToFirst()
then in this case it will point Lalit, as it is the first data in your cursor. To get the next data from cursor you can usemoveToNext()
.moveToLast()
- This will point Chandra as the current data in your cursor. To get the previous data from cursor you can usemoveToPrevious()