Showing posts with label Cursor. Show all posts
Showing posts with label Cursor. Show all posts

Cursor.moveToNext - Move the cursor to the next row

Cursor.moveToNext was added in API Level 1

public abstract boolean moveToNext ()
Move the cursor to the next row. This method will return false if the cursor is already past the last entry in the result set.
Returns: whether the move succeeded.

1. Move the Cursor to the First Row. 

2. In the MainActivity.java file, in the oncCreate method,

if (myCursor.moveToNext()) {
  •     // myCursor moved to the next record
    } else {
        // myCursor cursor is already past the last entry in the result set.
    }
3. Compile and run!

Resources:
http://developer.android.com/reference/android/database/Cursor.html#moveToNext()

Cursor getColumnIndexOrThrow - Get the Index for a Column Name

Cursor getColumnIndexOrThrow was added in API Level 1

public abstract int getColumnIndexOrThrow (String columnName)
Returns the zero-based index for the given column name, or throws IllegalArgumentException if the column doesn't exist. If you're not sure if a column will exist or not use getColumnIndex(String) and check for -1, which is more efficient than catching the exceptions.
Parameters: columnName the name of the target column.
Returns: the zero-based column index for the given column name
Throws: IllegalArgumentException if the column does not exist

1. Move the Cursor to the First Row of a Cursor named myCursor.

2. Add the below code right after if (myCursor.moveToFirst()) { where the // myCursor moved to the first record comment is located. This will save the contents of the Display name of an audio file to a string named myString. 

try {    String myString = cursor.getString(cursor.getColumnIndexOrThrow(android.provider.MediaStore.Audio.Media.DISPLAY_NAME));
} catch (IllegalArgumentException e ) { 
e.printStackTrace();
}

3. Compile and run!

4. Other column names for MediaStore Audio files are listed here. 

Resources:
http://developer.android.com/reference/android/database/Cursor.html#getColumnIndexOrThrow(java.lang.String)

Cursor getInt - Get the value of an Column as an Integer

Cursor getInt was added in API Level 1
public abstract int getInt (int columnIndex)

Returns the value of the requested column as an int.
The result and whether this method throws an exception when the column value is null, the column type is not an integral type, or the integer value is outside the range [Integer.MIN_VALUE, Integer.MAX_VALUE] is implementation-defined.
Parameters: columnIndex the zero-based index of the target column.
Returns: the value of that column as an int.

1. Move the Cursor to the First Row of a Cursor named myCursor.

2. Add the below line right after if (myCursor.moveToFirst()) { where the // myCursor moved to the first record comment is located. This will save the contents of the column _ID of an audio file to an integer named myInt. 

int myInt = cursor.getInt(cursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID));

3. Compile and run! 

4. Other column names for MediaStore Audio files are listed here. 

http://developer.android.com/reference/android/database/Cursor.html#getInt(int)

Cursor getColumnIndex - Get the Index for a Column Name

Cursor getColumnIndex was added in API Level 1

** Use getColumnIndexOrThrow(String) instead, if you expect the column to exist. **
public abstract int getColumnIndex (String columnName)
Returns the zero-based index for the given column name, or -1 if the column doesn't exist. If you expect the column to exist use getColumnIndexOrThrow(String) instead, which will make the error more clear.
Parameters: columnName the name of the target column.
Returns: the zero-based column index for the given column name, or -1 if the column name does not exist.

1. Move the Cursor to the First Row of a Cursor named myCursor.

2. Add the below line right after if (myCursor.moveToFirst()) { where the // myCursor moved to the first record comment is located. This will save the contents of the Display name of an audio file to a string named myString. 

String myString = cursor.getString(cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DISPLAY_NAME));

3. Compile and run!

4. Other column names for MediaStore Audio files are listed here. 

Resources:
http://developer.android.com/reference/android/database/Cursor.html#getColumnIndex(java.lang.String)

Cursor getString - Get the value of Column as a String

Cursor getString was added in API Level 1

public abstract String getString (int columnIndex)
Returns the value of the requested column as a String.
The result and whether this method throws an exception when the column value is null or the column type is not a string type is implementation-defined.
Parameters: columnIndex the zero-based index of the target column.
Returns: the value of that column as a String.

1. Move the Cursor to the First Row of a Cursor named myCursor.

2. Add the below line right after if (myCursor.moveToFirst()) { where the // myCursor moved to the first record comment is located. This will save the contents of the Display name of an audio file to a string named myString. 

String myString = cursor.getString(cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DISPLAY_NAME));

3. Compile and run! 

4. Other column names for MediaStore Audio files are listed here. 

Resources:
http://developer.android.com/reference/android/database/Cursor.html#getString(int)


Cursor moveToFirst - Move Cursor to the First Row

  • Cursor.moveToFirst () was added in API Level 1
    Move the cursor to the first row. This method will return false if the cursor is empty. Returns whether the move succeeded.

    1. Define a Cursor named myCursor.

    2. Load the Cursor using managedQuery(for API Level's < 11), otherwise use ContentLoader. 

  • 3. In the MainActivity. java file, on the onCreate method, add the following code.
  • if (myCursor != null) { //Tests to see if the cursor exists.
        if (myCursor.moveToFirst()) {
  •         // myCursor moved to the first record
        } else {
            // myCursor is empty
        }
  • } else {
        // myCursor is null
  • }

    4. Compile and run!

    Resources: 
  • http://developer.android.com/reference/android/database/Cursor.html#moveToFirst()

Deprecated: managedQuery

managedQuery (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

This method was deprecated in API level 11.
Use CursorLoader instead.

Wrapper around query(android.net.Uri, String[], String, String[], String) that gives the resulting Cursor to call startManagingCursor(Cursor) so that the activity will manage its lifecycle for you. If you are targeting HONEYCOMB(API 11) or later, consider instead using LoaderManager instead, available via getLoaderManager().

Warning: Do not call close() on a cursor obtained using this method, because the activity will do that for you at the appropriate time. However, if you call stopManagingCursor(Cursor) on a cursor from a managed query, the system will not automatically close the cursor and, in that case, you must call close().

Parameters
uri - The URI of the content provider to query.
projection - List of columns to return.
selection - SQL WHERE clause.
selectionArgs - The arguments to selection, if any ?s are present
sortOrder - SQL ORDER BY clause.

Returns: The Cursor that was returned by query().

1. Declare a Cursor.

2. In the MainActivity.java file, add the line below to the imports section. This is only needed in this example because we are using this as our URI.

import android.provider.MediaStore.Audio;

3. Declare and Define a String Array named myStringArray set to { "*" }.

4. In the MainActivity.java file, add the following line of code. This example sets
the Cursor named myCursor with query results from
a URI of android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
a list of all columns by using myStringArray,
a selection of android.provider.MediaStore.Audio.Media.IS_MUSIC + "!=0",
with the selection arguments and sort order both set to null.

myCursor = managedQuery(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, myStringArray, android.provider.MediaStore.Audio.Media.IS_MUSIC + " != 0", null, null); 

5. Compile and run!

Resources:
http://developer.android.com/reference/android/app/Activity.html#managedQuery(android.net.Uri, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String)


Declare a Cursor

How to Declare a Cursor

Cursor was added in API Level 1

This interface provides random read-write access to the result set returned by a database query.
Cursor implementations are not required to be synchronized so code using a Cursor from multiple threads should perform its own synchronization when using the Cursor.

1. Create an Android project, if you don't already have one.

2. In the MainActivity.java file, add the below import.

import android.database.Cursor;

3. After the public class and before the onCreate method, declare the Cursor and name it myCursor.

Cursor myCursor; 

4. Compile and run!

Resources:
http://developer.android.com/reference/android/database/Cursor.html

android:cursor - Make the Cursor Invisible in a TextView

How to Make the Cursor Invisible in a TextView

android:cursor was added in API Level 1

Makes the cursor visible (the default) or invisible. Note that this property only makes sense for editable TextView.
Aka, How to hide the cursor

1. Add a TextView.

2. In the main.xml file, add the below line

  <TextView
    ....
      android:cursor = "false" 
  />
 
3. Compile and run!

Resources: