Example usage for android.database Cursor registerContentObserver

List of usage examples for android.database Cursor registerContentObserver

Introduction

In this page you can find the example usage for android.database Cursor registerContentObserver.

Prototype

void registerContentObserver(ContentObserver observer);

Source Link

Document

Register an observer that is called when changes happen to the content backing this cursor.

Usage

From source file:com.getbase.android.db.loaders.ComposedCursorLoader.java

private Cursor loadCursorInBackground() {
    Cursor cursor = getContext().getContentResolver().query(mUri, mProjection, mSelection, mSelectionArgs,
            mSortOrder);//from   ww w . j  a  va 2 s . c o  m
    if (cursor != null) {
        // Ensure the cursor window is filled
        cursor.getCount();
        cursor.registerContentObserver(mObserver);
    }
    return cursor;
}

From source file:org.hisp.dhis.mobile.datacapture.io.AbsCursorLoader.java

@Override
public CursorHolder<T> loadInBackground() {
    Cursor cursor = getContext().getContentResolver().query(mUri, mProjection, mSelection, mSelectionArgs,
            mSortOrder);/* ww w.ja  va  2  s  .co  m*/
    if (cursor != null) {
        // Ensure the cursor window is filled
        cursor.getCount();
        cursor.registerContentObserver(mObserver);
    }
    // read data from cursor here
    return new CursorHolder<T>(cursor, readDataFromCursor(cursor));
}

From source file:com.example.iotalk.ObjectCursorLoader.java

@Override
public ObjectCursor<T> loadInBackground() {
    final Cursor inner = getContext().getContentResolver().query(mUri, mProjection, mSelection, mSelectionArgs,
            mSortOrder);//from w  w  w . ja  va2  s  .c  om
    if (inner != null) {
        // Ensure the cursor window is filled
        inner.getCount();
        inner.registerContentObserver(mObserver);
    }
    // Modifications to the ObjectCursor, create an Object Cursor and fill the cache.
    final ObjectCursor<T> cursor = getObjectCursor(inner);
    cursor.fillCache();

    try {
        if (mDebugDelayMs > 0) {
            Thread.sleep(mDebugDelayMs);
        }
    } catch (InterruptedException e) {
    }

    return cursor;
}

From source file:org.mariotaku.twidere.loader.support.ObjectCursorLoader.java

@Override
public ObjectCursor<T> loadInBackground() {
    Cursor cursor = getContext().getContentResolver().query(mUri, mProjection, mSelection, mSelectionArgs,
            mSortOrder);//w  ww  .  j a va 2 s.  co  m
    if (cursor != null) {
        // Ensure the cursor window is filled
        cursor.getCount();
        cursor.registerContentObserver(mObserver);
    }
    if (cursor == null)
        throw new NullPointerException("Cursor is null");
    return new ObjectCursor<>(cursor, createIndices(cursor));
}

From source file:org.mariotaku.twidere.loader.ObjectCursorLoader.java

@Override
public ObjectCursor<T> loadInBackground() {
    Cursor cursor = getContext().getContentResolver().query(mUri, mProjection, mSelection, mSelectionArgs,
            mSortOrder);// w  ww  .  j a v a  2s.  c o m
    if (cursor != null) {
        // Ensure the cursor window is filled
        cursor.getCount();
        cursor.registerContentObserver(mObserver);
    }
    if (cursor == null)
        throw new NullPointerException("Cursor is null");
    return createObjectCursor(cursor, createIndices(cursor));
}

From source file:com.vokal.db.util.ObjectCursorLoader.java

@Override
public ObjectCursor<T> loadInBackground() {
    final Cursor inner = getContext().getContentResolver().query(mUri, mProjection, mSelection, mSelectionArgs,
            mSortOrder);/* w w w  . jav  a 2s .  co  m*/
    if (inner == null) {
        return null;
    }

    // Ensure the cursor window is filled
    inner.getCount();
    inner.registerContentObserver(mObserver);

    // Modifications to the ObjectCursor, create an Object Cursor and fill the cache.
    final ObjectCursor<T> cursor = getObjectCursor(inner);
    cursor.fillCache();

    try {
        if (mDebugDelayMs > 0) {
            Thread.sleep(mDebugDelayMs);
        }
    } catch (InterruptedException e) {
    }

    return cursor;
}

From source file:es.jpv.android.examples.loadersexample.db.DBSelectCursorLoader.java

/**
 * Runs a query asynchronously//from   ww  w .  j av a  2s.  c o  m
 *
 * @return A Cursor object with the data
 */
@Override
public Cursor loadInBackground() {
    Cursor cursor = mDB.getReadableDatabase().query(mTable, mProjection, mSelection, mSelectionArgs, null, null,
            mSortOrder, String.valueOf(mLimit));
    if (cursor != null) {
        // Ensure the cursor window is filled
        cursor.getCount();
        cursor.registerContentObserver(mObserver);
    }
    return cursor;
}

From source file:com.bitants.wally.fragments.SavedImagesFragment.java

private void initObserver(Cursor cursor) {
    if (cursor != null) {
        if (contentObserver == null) {
            contentObserver = new ContentObserver(uiHandler) {
                @Override/*  w  w w  .j  a  v a  2s .  c o  m*/
                public void onChange(boolean selfChange) {
                    super.onChange(selfChange);
                    getImages(0, null);
                }
            };

        }
        try {
            cursor.registerContentObserver(contentObserver);
        } catch (IllegalStateException e) {
            //Oh goodie, it's already registered. Nevermind then.
        }
    }
}

From source file:cn.tangxb.imageselector.PhotoModelTaskLoader.java

@Override
public ArrayList<PhotoModel> loadInBackground() {
    synchronized (this) {
        if (isLoadInBackgroundCanceled()) {
            throw new OperationCanceledException();
        }/*from  ww w  .ja va2s  .c om*/
        mCancellationSignal = new CancellationSignal();
    }
    try {
        Cursor cursor = ContentResolverCompat.query(getContext().getContentResolver(),
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[] { MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.DATE_ADDED,
                        MediaStore.Images.ImageColumns.SIZE },
                null, null, MediaStore.Images.ImageColumns.DATE_ADDED, mCancellationSignal);
        if (cursor != null) {
            try {
                // Ensure the cursor window is filled.
                cursor.getCount();
                cursor.registerContentObserver(mObserver);
                mData = new ArrayList<>();

                if (cursor == null || !cursor.moveToNext())
                    return mData;
                cursor.moveToLast();
                do {
                    if (cursor.getLong(cursor.getColumnIndex(MediaStore.Images.ImageColumns.SIZE)) > 1024
                            * 10) {
                        PhotoModel photoModel = new PhotoModel();
                        photoModel.setOriginalPath(
                                cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)));
                        mData.add(photoModel);
                    }
                } while (cursor.moveToPrevious());
            } catch (RuntimeException ex) {
                cursor.close();
                throw ex;
            }
        }
        return mData;
    } finally {
        synchronized (this) {
            mCancellationSignal = null;
        }
    }
}

From source file:uk.co.senab.photup.util.CursorPagerAdapter.java

/**
 * Swap in a new Cursor, returning the old Cursor. Unlike
 * {@link #changeCursor(Cursor)}, the returned old Cursor is <em>not</em>
 * closed.// w  w w  .  j a v a 2  s  . co m
 * 
 * @param newCursor
 *            The new cursor to be used.
 * @return Returns the previously set Cursor, or null if there was not one.
 *         If the given new Cursor is the same instance is the previously
 *         set Cursor, null is also returned.
 */
public Cursor swapCursor(Cursor newCursor) {
    if (newCursor == mCursor) {
        return null;
    }
    Cursor oldCursor = mCursor;
    if (oldCursor != null) {
        if (mChangeObserver != null)
            oldCursor.unregisterContentObserver(mChangeObserver);
        if (mDataSetObserver != null)
            oldCursor.unregisterDataSetObserver(mDataSetObserver);
    }
    mCursor = newCursor;
    if (newCursor != null) {
        if (mChangeObserver != null)
            newCursor.registerContentObserver(mChangeObserver);
        if (mDataSetObserver != null)
            newCursor.registerDataSetObserver(mDataSetObserver);
        mRowIDColumn = newCursor.getColumnIndexOrThrow("_id");
        mDataValid = true;
        // notify the observers about the new cursor
        notifyDataSetChanged();
    } else {
        mRowIDColumn = -1;
        mDataValid = false;
        // notify the observers about the lack of a data set
        notifyDataSetChanged();
    }
    return oldCursor;
}