The concept and usage of the cursor class in Android

Source: Internet
Author: User

Every day to learn a lot of things, a lot of things do not know, had to Baidu. Make your own blog into a file vault

Original link: http://www.2cto.com/kf/201109/103163.html

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 the cursor and release the resource
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 the cursor to the first line
Movetolast ()--Move the cursor to the last line
MoveToNext ()--Move the cursor to the next line
movetoposition (int position)--moves the cursor to an absolute position
movetoprevious ()--Move the cursor to the previous line

Let's look at a short piece of code:

if (cur.movetofirst () = = False) {//empty cursor
return;
}

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

String email = cursor.getString(cursor.getColumnIndex(RuiXin.EMAIL));

   startManagingCursor(cursor);  //查找后关闭游标 
//Remove the data
}


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 last row of the page
isclosed ()--returns true to indicate that the game has been 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.

The Cursor is located in the Android.database.Cursor class, which shows that its design is based on the database service.

Above transfer from: http://www.2cto.com/kf/201109/103163.html

Another: Activity. Startmanagingcursor Method:

The cursor object is handed to the activity management so that the life cycle of the cursor object can be automatically synchronized with the current activity, eliminating the ability to manage the cursor.

1. This method uses the premise that there are a lot of data records in the cursor result set.

Therefore, before using, the cursor is NULL to determine if the cursor = NULL, and then use this method 2. If you use this method, you will end up using Stopmanagingcursor () to stop the error. 3. The purpose of this method is to give the retrieved cursor object to activity management so that the cursor's life cycle can be automatically synchronized with the activity, saving itself from manual management.

The concept and usage of the cursor class in Android

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.