This post was reproduced from: http://www.cnblogs.com/TerryBlog/archive/2010/07/05/1771459.html
Use of the cursor in the presenter
Children's shoes using SQLite database should be no stranger to cursor, if you are engaged in. NET development you can interpret the cursor as a collection of data in ADO, which is equivalent to DataReader. Today, I'm going to take it out alone to deepen my understanding of using Cursor in Android.
About Cursor
When you understand and use the Android cursor, you must first know a few things about the cursor:
 
 
  
  - The Cursor is a collection of each row.
- Use Movetofirst () to locate the first row.
- You must know the name of each column.
- You must know the data type of each column.
- The Cursor is a random data source.
- All data is obtained by subscript.
important methods for Cursor :
 
 
 
 - Close ()
 Close cursors, freeing resources
- Copystringtobuffer (int columnindex, chararraybuffer buffer)
 Retrieves the text of the requested column in the buffer, storing it
- getColumnCount ()
 Returns the total number of all columns
- Getcolumnindex (String columnName)
 Returns the name of the specified column if there is no return-1
- Getcolumnindexorthrow (String columnName)
 Returns the specified column name from zero, and throws a illegalargumentexception exception if it does not exist.
- 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 the cursor
- Movetofirst ()
 Move cursor to First line
- Movetolast ()
 Move the cursor to the last line
- MoveToNext ()
 Move the cursor to the next line
- movetoposition (int position)
 Move the cursor to an absolute position
- Movetoprevious ()
 Move cursor to previous line
Let's look at a short piece of code:
if (cur.movetofirst () = = False)
{
Cursorreturn for the empty;
}
Access the Cursor's subscript to get the data
int namecolumnindex = Cur.getcolumnindex (people.name);
String name = cur.getstring (Namecolumnindex);
Now let's see how we loop the Cursor out of the data we need.
while (Cur.movetonext ())
{
Cursor moved successfully
Get the data out.
}
When Cur.movetonext () is false, it jumps out of the loop, that is, the Cursor data loop is complete.
If you prefer using a for loop instead of a while loop, you can use the few methods that Google provides:
 
 
  
  - Isbeforefirst ()
 Returns whether the cursor points to the position of the previous first row
- Isafterlast ()
 Returns whether the cursor points to the position of the last row
- IsClosed ()
 Returns true to indicate that the game is marked off
With the above method, you can remove the data
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: Querying data on Android is done through the cursor class. When we use the Sqlitedatabase.query () method, we get the cursor object, and the cursor points to each piece of data. It may be a good idea to combine the knowledge of ADO.
The Cursor is located in the Android.database.Cursor class, which shows that its design is based on the database service.
In addition, there are several known sub-categories, namely:
 
 
  
  - Abstractcursor
- Abstractwindowedcursor
- Crossprocesscursor
- Cursorwrapper
- Matrixcursor
- Mergecursor
- Mockcursor
- Sqlitecursor
Detailed use of the method and interpretation can refer to the API, here is not too much to tell.
Introduction to the "Cursor" class in Android