Those who have used SQLite databases should be familiar with Cursor. If you are engaged in. net development, you can understand Cursor as a data set in Ado.net, which is equivalent to dataReader. Today, I will talk about it separately to deepen my understanding of Using Cursor in Android.
 
About Cursor
 
When you understand and use Android Cursor, you must first know several things about Cursor:
 
Cursor is a set of rows.
Use moveToFirst () to locate the first line.
You must know the name of each column.
You must know the Data Type of each column.
Cursor is a random data source.
All data is obtained by subscript.
An important method for Cursor:
 
Close ()
Close the cursor to release resources
CopyStringToBuffer (int columnIndex, CharArrayBuffer buffer)
Retrieve the text of the requested column in the buffer and store it
GetColumnCount ()
Returns the total number of all columns.
GetColumnIndex (String columnName)
Returns the name of the specified column. If no Column exists,-1 is returned.
GetColumnIndexOrThrow (String columnName)
The specified column name is returned from scratch. If the column name does not exist, an IllegalArgumentException exception is thrown.
GetColumnName (int columnIndex)
Returns the column name from the given index.
GetColumnNames ()
Returns the column name of a string array.
GetCount ()
Returns the number of rows in Cursor.
MoveToFirst ()
Move the cursor to the first line
MoveToLast ()
Move the cursor to the last line
MoveToNext ()
Move the cursor to the next row
MoveToPosition (int position)
Move the cursor to an absolute position
MoveToPrevious ()
Move the cursor to the previous line
Let's take a look at a short piece of code:
 
 
 
If (cur. moveToFirst () = false)
{
// Empty Cursor
Return;
}
 
 
 
Access the subscript of Cursor to obtain the data.
 
Int nameColumnIndex = cur. getColumnIndex (People. NAME );
String name = cur. getString (nameColumnIndex );
Now let's take a look at how to cyclically extract the data we need from Cursor.
 
While (cur. moveToNext ())
{
// The cursor is successfully moved.
// Extract data
}
 
When cur. moveToNext () is false, the loop will jump out, that is, the Cursor data loop is complete.
 
If you prefer to use a for loop instead of A While LOOP, you can use the following methods provided by Google:
 
IsBeforeFirst ()
Returns whether the cursor points to the position of the first row.
IsAfterLast ()
Returns whether the cursor points to the position of the last row.
IsClosed ()
If true is returned, the game logo is disabled.
With the above method, data can be retrieved in this way
 
For (cur. moveToFirst ();! Cur. isAfterLast (); cur. moveToNext ())
{
Int nameColumn = cur. getColumnIndex (People. NAME );
Int phoneColumn = cur. getColumnIndex (People. NUMBER );
String name = cur. getString (nameColumn );
String phoneNumber = cur. getString (phoneColumn );
}
 
Tip: data query in Android is implemented through the Cursor class. When we use the SQLiteDatabase. query () method, we will get the Cursor object, which points to each piece of data. The knowledge of ADO.net may be better understood.
 
The Cursor is located in the android. database. Cursor class. It can be seen that its design is based on the database service.
 
Author's "kbest column"