Example usage for android.database Cursor registerDataSetObserver

List of usage examples for android.database Cursor registerDataSetObserver

Introduction

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

Prototype

void registerDataSetObserver(DataSetObserver observer);

Source Link

Document

Register an observer that is called when changes happen to the contents of the this cursors data set, for example, when the data set is changed via #requery() , #deactivate() , or #close() .

Usage

From source file:com.github.monxalo.android.widget.SectionCursorAdapter.java

@Override
public Cursor swapCursor(Cursor newCursor) {
    if (getCursor() != null) {
        getCursor().unregisterDataSetObserver(mDataSetObserver);
    }/*from  ww  w . j av  a2 s .c om*/

    final Cursor oldCursor = super.swapCursor(newCursor);

    calculateSectionHeaders();

    if (newCursor != null) {
        newCursor.registerDataSetObserver(mDataSetObserver);
    }

    return oldCursor;
}

From source file:com.monxalo.android.widget.SectionCursorAdapter.java

public SectionCursorAdapter(Context context, Cursor c, int headerLayout, int groupColumn) {
    super(context, c, 0);

    sectionsIndexer = new LinkedHashMap<Integer, String>();

    mHeaderRes = headerLayout;/*  ww w  .  j ava 2 s. c  o  m*/
    mGroupColumn = groupColumn;
    mLayoutInflater = LayoutInflater.from(context);

    if (c != null) {
        calculateSectionHeaders();
        c.registerDataSetObserver(mDataSetObserver);
    }
}

From source file:com.github.monxalo.android.widget.SectionCursorAdapter.java

public SectionCursorAdapter(Context context, Cursor c, int headerLayout, int groupColumn) {
    super(context, c, 0);

    mSectionsIndexer = new SparseArray<String>();

    mHeaderRes = headerLayout;//from   w w  w.  j  a v  a 2 s. c  o m
    mGroupColumn = groupColumn;
    mLayoutInflater = LayoutInflater.from(context);

    if (c != null) {
        calculateSectionHeaders();
        c.registerDataSetObserver(mDataSetObserver);
    }
}

From source file:com.github.monxalo.android.widget.SectionCursorAdapter.java

public SectionCursorAdapter(Context context, Cursor c, int headerLayout, String columnName) {
    super(context, c, 0);

    mSectionsIndexer = new SparseArray<String>();

    mHeaderRes = headerLayout;/*from   w w  w  .  j a v a2 s  .  co  m*/
    mGroupColumn = c.getColumnIndex(columnName);
    mLayoutInflater = LayoutInflater.from(context);

    if (c != null) {
        calculateSectionHeaders();
        c.registerDataSetObserver(mDataSetObserver);
    }
}

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.//from w  w w . ja va  2 s  .c o 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;
}

From source file:com.cattle.util.CursorPagerAdapter.java

/**
 * Swap in a new Cursor, returning the old Cursor. Unlike {@link #changeCursor(android.database.Cursor)}, the
 * returned old Cursor is <em>not</em> closed.
 *
 * @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.
 *//*from  w w  w . ja  va2  s .com*/
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;
}

From source file:org.opendatakit.services.instance.provider.InstanceProvider.java

@Override
public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs,
        String sortOrder) {//from   w  ww .j a  v a2s . co  m
    possiblyWaitForContentProviderDebugger();

    List<String> segments = uri.getPathSegments();

    if (segments.size() < 2 || segments.size() > 3) {
        throw new SQLException("Unknown URI (too many segments!) " + uri);
    }

    String appName = segments.get(0);
    ODKFileUtils.verifyExternalStorageAvailability();
    ODKFileUtils.assertDirectoryStructure(appName);
    String tableId = segments.get(1);

    DbHandle dbHandleName = OdkConnectionFactorySingleton.getOdkConnectionFactoryInterface()
            .generateInternalUseDbHandle();

    boolean success = false;
    OdkConnectionInterface db = null;
    try {
        // +1 referenceCount if db is returned (non-null)
        db = OdkConnectionFactorySingleton.getOdkConnectionFactoryInterface().getConnection(appName,
                dbHandleName);

        Cursor c = db.query(tableId, projection, selection, selectionArgs, null, null, sortOrder, null);

        if (c == null) {
            return null;
        }

        // Tell the cursor what uri to watch, so it knows when its source data
        // changes
        c.setNotificationUri(getContext().getContentResolver(), uri);
        c.registerDataSetObserver(new InvalidateMonitor(appName, dbHandleName));
        success = true;
        return c;
    } finally {
        if (db != null) {
            try {
                db.releaseReference();
            } finally {
                if (!success) {
                    // this closes the connection
                    // if it was successful, then the InvalidateMonitor will close the connection
                    OdkConnectionFactorySingleton.getOdkConnectionFactoryInterface().removeConnection(appName,
                            dbHandleName);
                }
            }
        }
    }
}

From source file:com.niveales.library.utils.MyCursorAdapter.java

/**
 * Swap in a new Cursor, returning the old Cursor.  Unlike
 * {@link #changeCursor(Cursor)}, the returned old Cursor is <em>not</em>
 * closed./*from   w  w  w.  java 2  s. c  om*/
 *
 * @param newCursor The new cursor to be used.
 * @return Returns the previously set Cursor, or null if there wasa 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_field);
        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
        notifyDataSetInvalidated();
    }
    return oldCursor;
}

From source file:com.cattle.util.CursorPagerAdapter.java

void init(Context context, Cursor c, int flags) {
    if ((flags & FLAG_AUTO_REQUERY) == FLAG_AUTO_REQUERY) {
        flags |= FLAG_REGISTER_CONTENT_OBSERVER;
        mAutoRequery = true;//from  ww w  .j a  va 2s  . co m
    } else {
        mAutoRequery = false;
    }
    boolean cursorPresent = c != null;
    mCursor = c;
    mDataValid = cursorPresent;
    mContext = context;
    mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1;
    if ((flags & FLAG_REGISTER_CONTENT_OBSERVER) == FLAG_REGISTER_CONTENT_OBSERVER) {
        mChangeObserver = new ChangeObserver();
        mDataSetObserver = new MyDataSetObserver();
    } else {
        mChangeObserver = null;
        mDataSetObserver = null;
    }

    if (cursorPresent) {
        if (mChangeObserver != null) {
            c.registerContentObserver(mChangeObserver);
        }
        if (mDataSetObserver != null) {
            c.registerDataSetObserver(mDataSetObserver);
        }
    }
}

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

void init(Context context, Cursor c, int flags) {
    if ((flags & FLAG_AUTO_REQUERY) == FLAG_AUTO_REQUERY) {
        flags |= FLAG_REGISTER_CONTENT_OBSERVER;
        mAutoRequery = true;/*  www . j a va 2 s . c om*/
    } else {
        mAutoRequery = false;
    }
    boolean cursorPresent = c != null;
    mCursor = c;
    mDataValid = cursorPresent;
    mContext = context;
    mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1;
    if ((flags & FLAG_REGISTER_CONTENT_OBSERVER) == FLAG_REGISTER_CONTENT_OBSERVER) {
        mChangeObserver = new ChangeObserver();
        mDataSetObserver = new MyDataSetObserver();
    } else {
        mChangeObserver = null;
        mDataSetObserver = null;
    }

    if (cursorPresent) {
        if (mChangeObserver != null)
            c.registerContentObserver(mChangeObserver);
        if (mDataSetObserver != null)
            c.registerDataSetObserver(mDataSetObserver);
    }
}