The cursor class for Android notes

Source: Internet
Author: User

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 () Closes the cursor, freeing 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 cursor to 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 cursor to previous line

Determine if the cursor is empty

if false ) {// empty cursorreturn;}

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 // pull 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 has been marked off

With the above method, you can remove the data

For! 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); }

Done

The cursor class for Android notes

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.