Adv

Android Cursor

Android Cursor Example and Tutorial.

 

1. How to Close a Cursor

    /**
     * Closes the passed in {@link Cursor}.
     *
     * @param cursor The {@link Cursor} to be closed.
     */
    public static void close(final Cursor cursor) {
        if ((cursor != null) && (!cursor.isClosed())) {
            cursor.close();
        }
    }

2. How to determine if a Cursor has Data

    /**
     * Determines if the passed in {@link Cursor} contains any data.
     *
     * @param cursor    The {@link Cursor} that will be checked.
     * @param moveFirst Indicates if the {@link Cursor} has data, move to the first record.
     *
     * @return A {@link Boolean} indicating whether the passed in {@link Cursor} has data.
     */
    public static boolean hasData(final Cursor cursor, final boolean moveFirst) {
        boolean success;

        try {
            success = (cursor != null && cursor.getCount() > 0);
            if (success && moveFirst) {
                success = cursor.moveToFirst();
            }
        }
        catch (Exception ex) {
            success = false;
        }

        return success;
    }
Share
Adv



Share an Example

Share an Example

Browse
What is the capital of Egypt? ( Cairo )