Code example for Cursor

Methods: closedeactivate

0
            // IllegalStateException may be thrown if the cursor is stale. 
            Log.e(TAG, "Caught exception while deactivating cursor.", e);
        } 
        mContentResolver = null;
        if (mCursor != null) {
            mCursor.close();
            mCursor = null;
        } 
    } 
 
    // TODO: Change public to protected 
    public Uri contentUri(long id) {
        // TODO: avoid using exception for most cases 
        try { 
            // does our uri already have an id (single image query)? 
            // if so just return it 
            long existingId = ContentUris.parseId(mBaseUri);
            if (existingId != id) Log.e(TAG, "id mismatch");
            return mBaseUri;
        } catch (NumberFormatException ex) {
            // otherwise tack on the id 
            return ContentUris.withAppendedId(mBaseUri, id);
        } 
    } 
 
    public int getCount() { 
        Cursor cursor = getCursor();
        if (cursor == null) return 0;
        synchronized (this) {
            return cursor.getCount();
        } 
    } 
 
    public boolean isEmpty() { 
        return getCount() == 0; 
    } 
 
    private Cursor getCursor() {
        synchronized (this) {
            if (mCursor == null) return null;
            if (mCursorDeactivated) {
                mCursor.requery();
                mCursorDeactivated = false;
            } 
            return mCursor;
        } 
    } 
 
    public IImage getImageAt(int i) {
        BaseImage result = mCache.get(i);
        if (result == null) {
            Cursor cursor = getCursor();
            if (cursor == null) return null;
            synchronized (this) {
                result = cursor.moveToPosition(i)
                        ? loadImageFromCursor(cursor)
                        : null; 
                mCache.put(i, result);
            } 
        } 
        return result;
    } 
 
    protected abstract Cursor createCursor();
 
    protected abstract BaseImage loadImageFromCursor(Cursor cursor);
 
    protected void invalidateCursor() { 
        if (mCursor == null) return;
        mCursor.deactivate();
        mCursorDeactivated = true;
    } 
 
    // This provides a default sorting order string for subclasses. 
    // The list is first sorted by date, then by id. The order can be ascending 
Stop searching for code, let great code find you!  Add Codota to your java IDE