Reproduced:
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;
}Copy Code
Access the Cursor's subscript to get the data
int namecolumnindex = Cur.getcolumnindex (people.name);
String name = cur.getstring (Namecolumnindex);Copy Code
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.
}Copy Code
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);
}Copy Code
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
Matrixcursorcontentprovider the query () method when sharing data externally requires a cursor.
But when ContentProvider is not data from the database, it needs to be matrixcursor when it needs to return to the cursor.
ContentProvider the query () method when sharing data externally requires a cursor, but what if there is no database and the project needs to read data from ContentProvider? What's worse is that the cursor is also required for all other methods of operation. It's time to matrixcursor. Quite interesting, it's equivalent to simulating a table for you.
Public Cursor query (Uri uri, string[] projection, string selection, string[] Selectionargs, string sortOrder) { New string[] {"Name", "Job", "Salary" }; New matrixcursor (tablecursor); Cursor.addrow (new object[] {"1111", "1111", "1111" }); return cursor;}
Cursor class in Android