What is use of Cursor in Android Development?

RMH picture RMH · Mar 30, 2012 · Viewed 68.5k times · Source

I was going through some of the codes on the internet regarding the database connection, retrieval. I saw Cursor cur1= moveToFirst() in many codes, I wanted to know what is the use of a cursor and why we use moveToFirst() as I am new to android.

Answer

Chandra Sekhar picture Chandra Sekhar · Mar 30, 2012

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