Cursors And Content Values

ContentValues are used to insert new rows into tables. Each Content Values object represents a single table row as a map of column names to values.

Queries in Android are returned as Cursor objects. Rather than extracting and returning a copy of the result values, Cursors are pointers to the result set within the underlying data. Cursors provide a managed way of controlling your position (row) in the result set of a database query.

The Cursor class includes a number of navigation functions including, but not limited to, the following:

> moveToFirst Moves the cursor to the first row in the query result

> moveToNext Moves the cursor to the next row

> moveToPrevious Moves the cursor to the previous row

> getCount Returns the number of rows in the result set

> getColumnIndexOrThrow Returns the index for the column with the specified name (throwing an exception if no column exists with that name)

> getColumnName Returns the name of the specified column index

> getColumnNames Returns a string array of all the column names in the current Cursor

> moveToPosition Moves the Cursor to the specified row

> getPosition Returns the current Cursor position

Android provides a convenient mechanism for simplifying the management of Cursors within your Activities. The startManagingCursor method integrates the Cursor's lifetime into the calling Activity's. When you've finished with the Cursor, call stopManagingCursor to do just that.

Later in this chapter you'll learn how to query a database and how to extract specific row/column values from the resulting Cursors.

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide

Post a comment