Use of cursor in android

Source: Internet
Author: User

Android uses the SQLite database. You can use Cursor to perform database record operations.

1. 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.
The Cursor is located in the android. database. Cursor class. It can be seen that its design is based on the database service.
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.


2. 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

3. Small Example:
(1) Judgment of empty Cursor
If (cur. moveToFirst () = false)
{
// Empty Cursor
Return;
}

(2) access the subscript of Cursor to obtain the data.

Int nameColumnIndex = cur. getColumnIndex (People. NAME );
String name = cur. getString (nameColumnIndex );

(3) loop Cursor to retrieve the required data

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, you can retrieve the data as follows:

AbstractCursor
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 );
}

4. In addition, there are several sub-classes known:

Abstract1_wedcursor
CrossProcessCursor
CursorWrapper
MatrixCursor
MergeCursor
MockCursor
SQLiteCursor

5. Refer to the RingtoneManager. java (which belongs to the framework layer) in the Android source code and the SoundRecorder application to understand how they are used.

 


From TQUDING's column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.