Java Code Examples for android.database.Cursor.registerContentObserver()

The following are Jave code examples for showing how to use registerContentObserver() of the android.database.Cursor class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: CSipSimple   File: GroupingListAdapter.java   Source Code and License Vote up 6 votes
public void changeCursor(Cursor cursor) {
    if (cursor == mCursor) {
        return;
    }

    if (mCursor != null) {
        mCursor.unregisterContentObserver(mChangeObserver);
        mCursor.unregisterDataSetObserver(mDataSetObserver);
        mCursor.close();
    }
    mCursor = cursor;
    resetCache();
    findGroups();

    if (cursor != null) {
        cursor.registerContentObserver(mChangeObserver);
        cursor.registerDataSetObserver(mDataSetObserver);
        mRowIdColumnIndex = cursor.getColumnIndexOrThrow("_id");
        notifyDataSetChanged();
    } else {
        // notify the observers about the lack of a data set
        notifyDataSetInvalidated();
    }

}
 
Example 2
Project: Ocr-android   File: BaseRecyclerCursorAdapter.java   Source Code and License Vote up 6 votes
private void init(Context context, Cursor c, int flags) {
    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);
        }
    }
    setHasStableIds(true);
}
 
Example 3
Project: SuperSelector   File: RecyclerViewCursorAdapter.java   Source Code and License Vote up 6 votes
void init(Context context, Cursor c, int flags) {

        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);
        }
        setHasStableIds(true);
    }
 
Example 4
Project: PeSanKita-android   File: AbstractCursorLoader.java   Source Code and License Vote up 5 votes
@Override
public Cursor loadInBackground() {
  Cursor newCursor = getCursor();
  if (newCursor != null) {
    newCursor.getCount();
    newCursor.registerContentObserver(observer);
  }
  return newCursor;
}
 
Example 5
Project: Ocr-android   File: BaseRecyclerCursorAdapter.java   Source Code and License Vote up 5 votes
/**
     * Swap in a new Cursor, returning the old Cursor.  Unlike
     * {@link #changeCursor(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 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");
            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();
//            notifyDataSetInvalidated();
        }
        return oldCursor;
    }
 
Example 6
Project: SuperSelector   File: RecyclerViewCursorAdapter.java   Source Code and License Vote up 5 votes
/**
     * Swap in a new Cursor, returning the old Cursor.  Unlike
     * {@link #changeCursor(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 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");
            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();
//            notifyDataSetInvalidated();
        }
        return oldCursor;
    }
 
Example 7
Project: letv   File: CursorLoader.java   Source Code and License Vote up 5 votes
public Cursor loadInBackground() {
    Cursor cursor = null;
    try {
        cursor = getContext().getContentResolver().query(this.mUri, this.mProjection, this.mSelection, this.mSelectionArgs, this.mSortOrder);
        if (cursor != null) {
            cursor.getCount();
            cursor.registerContentObserver(this.mObserver);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return cursor;
}
 
Example 8
Project: News24x7-news-from-every-part-of-the-world   File: CursorRecyclerViewAdapter.java   Source Code and License Vote up 5 votes
/**
 * Swap in a new Cursor, returning the old Cursor.  Unlike
 * {@link #changeCursor(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 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");
        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();
        notifyItemRangeRemoved(0, oldCursor.getCount());
    }
    return oldCursor;
}
 
Example 9
Project: letv   File: CursorAdapter.java   Source Code and License Vote up 5 votes
public Cursor swapCursor(Cursor newCursor) {
    if (newCursor == this.mCursor) {
        return null;
    }
    Cursor oldCursor = this.mCursor;
    if (oldCursor != null) {
        if (this.mChangeObserver != null) {
            oldCursor.unregisterContentObserver(this.mChangeObserver);
        }
        if (this.mDataSetObserver != null) {
            oldCursor.unregisterDataSetObserver(this.mDataSetObserver);
        }
    }
    this.mCursor = newCursor;
    if (newCursor != null) {
        if (this.mChangeObserver != null) {
            newCursor.registerContentObserver(this.mChangeObserver);
        }
        if (this.mDataSetObserver != null) {
            newCursor.registerDataSetObserver(this.mDataSetObserver);
        }
        this.mRowIDColumn = newCursor.getColumnIndexOrThrow("_id");
        this.mDataValid = true;
        notifyDataSetChanged();
        return oldCursor;
    }
    this.mRowIDColumn = -1;
    this.mDataValid = false;
    notifyDataSetInvalidated();
    return oldCursor;
}
 
Example 10
Project: letv   File: CursorLoader.java   Source Code and License Vote up 5 votes
public Cursor loadInBackground() {
    Cursor cursor;
    synchronized (this) {
        if (isLoadInBackgroundCanceled()) {
            throw new OperationCanceledException();
        }
        this.mCancellationSignal = new CancellationSignal();
    }
    try {
        cursor = ContentResolverCompat.query(getContext().getContentResolver(), this.mUri, this.mProjection, this.mSelection, this.mSelectionArgs, this.mSortOrder, this.mCancellationSignal);
        if (cursor != null) {
            cursor.getCount();
            cursor.registerContentObserver(this.mObserver);
        }
        synchronized (this) {
            this.mCancellationSignal = null;
        }
        return cursor;
    } catch (RuntimeException ex) {
        cursor.close();
        throw ex;
    } catch (Throwable th) {
        synchronized (this) {
            this.mCancellationSignal = null;
        }
    }
}
 
Example 11
Project: letv   File: CursorAdapter.java   Source Code and License Vote up 5 votes
void init(Context context, Cursor c, int flags) {
    boolean cursorPresent = true;
    if ((flags & 1) == 1) {
        flags |= 2;
        this.mAutoRequery = true;
    } else {
        this.mAutoRequery = false;
    }
    if (c == null) {
        cursorPresent = false;
    }
    this.mCursor = c;
    this.mDataValid = cursorPresent;
    this.mContext = context;
    this.mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1;
    if ((flags & 2) == 2) {
        this.mChangeObserver = new ChangeObserver();
        this.mDataSetObserver = new MyDataSetObserver();
    } else {
        this.mChangeObserver = null;
        this.mDataSetObserver = null;
    }
    if (cursorPresent) {
        if (this.mChangeObserver != null) {
            c.registerContentObserver(this.mChangeObserver);
        }
        if (this.mDataSetObserver != null) {
            c.registerDataSetObserver(this.mDataSetObserver);
        }
    }
}
 
Example 12
Project: letv   File: CursorAdapter.java   Source Code and License Vote up 5 votes
public Cursor swapCursor(Cursor newCursor) {
    if (newCursor == this.mCursor) {
        return null;
    }
    Cursor oldCursor = this.mCursor;
    if (oldCursor != null) {
        if (this.mChangeObserver != null) {
            oldCursor.unregisterContentObserver(this.mChangeObserver);
        }
        if (this.mDataSetObserver != null) {
            oldCursor.unregisterDataSetObserver(this.mDataSetObserver);
        }
    }
    this.mCursor = newCursor;
    if (newCursor != null) {
        if (this.mChangeObserver != null) {
            newCursor.registerContentObserver(this.mChangeObserver);
        }
        if (this.mDataSetObserver != null) {
            newCursor.registerDataSetObserver(this.mDataSetObserver);
        }
        this.mRowIDColumn = newCursor.getColumnIndexOrThrow("_id");
        this.mDataValid = true;
        notifyDataSetChanged();
        return oldCursor;
    }
    this.mRowIDColumn = -1;
    this.mDataValid = false;
    notifyDataSetInvalidated();
    return oldCursor;
}
 
Example 13
Project: News24x7-news-from-every-part-of-the-world   File: CursorRecyclerViewAdapter.java   Source Code and License Vote up 5 votes
void init(Cursor c) {
    boolean cursorPresent = c != null;
    mCursor = c;
    mDataValid = cursorPresent;
    mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1;

    mChangeObserver = new ChangeObserver();
    mDataSetObserver = new MyDataSetObserver();

    if (cursorPresent) {
        if (mChangeObserver != null) c.registerContentObserver(mChangeObserver);
        if (mDataSetObserver != null) c.registerDataSetObserver(mDataSetObserver);
    }
}
 
Example 14
Project: boohee_v5.6   File: CursorAdapter.java   Source Code and License Vote up 5 votes
void init(Context context, Cursor c, int flags) {
    boolean cursorPresent = true;
    if ((flags & 1) == 1) {
        flags |= 2;
        this.mAutoRequery = true;
    } else {
        this.mAutoRequery = false;
    }
    if (c == null) {
        cursorPresent = false;
    }
    this.mCursor = c;
    this.mDataValid = cursorPresent;
    this.mContext = context;
    this.mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1;
    if ((flags & 2) == 2) {
        this.mChangeObserver = new ChangeObserver();
        this.mDataSetObserver = new MyDataSetObserver();
    } else {
        this.mChangeObserver = null;
        this.mDataSetObserver = null;
    }
    if (cursorPresent) {
        if (this.mChangeObserver != null) {
            c.registerContentObserver(this.mChangeObserver);
        }
        if (this.mDataSetObserver != null) {
            c.registerDataSetObserver(this.mDataSetObserver);
        }
    }
}
 
Example 15
Project: boohee_v5.6   File: CursorAdapter.java   Source Code and License Vote up 5 votes
public Cursor swapCursor(Cursor newCursor) {
    if (newCursor == this.mCursor) {
        return null;
    }
    Cursor oldCursor = this.mCursor;
    if (oldCursor != null) {
        if (this.mChangeObserver != null) {
            oldCursor.unregisterContentObserver(this.mChangeObserver);
        }
        if (this.mDataSetObserver != null) {
            oldCursor.unregisterDataSetObserver(this.mDataSetObserver);
        }
    }
    this.mCursor = newCursor;
    if (newCursor != null) {
        if (this.mChangeObserver != null) {
            newCursor.registerContentObserver(this.mChangeObserver);
        }
        if (this.mDataSetObserver != null) {
            newCursor.registerDataSetObserver(this.mDataSetObserver);
        }
        this.mRowIDColumn = newCursor.getColumnIndexOrThrow("_id");
        this.mDataValid = true;
        notifyDataSetChanged();
        return oldCursor;
    }
    this.mRowIDColumn = -1;
    this.mDataValid = false;
    notifyDataSetInvalidated();
    return oldCursor;
}
 
Example 16
Project: Cable-Android   File: AbstractCursorLoader.java   Source Code and License Vote up 5 votes
@Override
public Cursor loadInBackground() {
  Cursor newCursor = getCursor();
  if (newCursor != null) {
    newCursor.getCount();
    newCursor.registerContentObserver(observer);
  }
  return newCursor;
}
 
Example 17
Project: q-mail   File: MergeCursor.java   Source Code and License Vote up 4 votes
@Override
public void registerContentObserver(ContentObserver observer) {
    for (Cursor cursor : mCursors) {
        cursor.registerContentObserver(observer);
    }
}
 
Example 18
Project: CodeWatch   File: TestProvider.java   Source Code and License Vote up 4 votes
public void testUpdateLeader() {
    // Create a new map of values, where column names are the keys
    ContentValues values = TestUtilities.createLeaderValues();

    Uri Uri = mContext.getContentResolver().
            insert(LeaderContract.LeaderEntry.CONTENT_URI, values);
    long leaderRowId = ContentUris.parseId(Uri);

    // Verify we got a row back.
    assertTrue(leaderRowId != -1);
    Log.d(LOG_TAG, "New row id: " + leaderRowId);

    ContentValues updatedValues = new ContentValues(values);
    updatedValues.put(LeaderContract.LeaderEntry._ID, leaderRowId);
    updatedValues.put(LeaderContract.LeaderEntry.COLUMN_USER_NAME, "Bob");

    // Create a cursor with observer to make sure that the content provider is notifying
    // the observers as expected
    Cursor leaderCursor = mContext.getContentResolver().query(
            LeaderContract.LeaderEntry.CONTENT_URI, null, null, null, null);

    TestUtilities.TestContentObserver tco = TestUtilities.getTestContentObserver();
    leaderCursor.registerContentObserver(tco);

    int count = mContext.getContentResolver().update(
            LeaderContract.LeaderEntry.CONTENT_URI, updatedValues, LeaderContract.LeaderEntry._ID + "= ?",
            new String[]{Long.toString(leaderRowId)});
    assertEquals(count, 1);

    // Test to make sure our observer is called.  If not, we throw an assertion.
    tco.waitForNotificationOrFail();

    leaderCursor.unregisterContentObserver(tco);
    leaderCursor.close();

    Cursor cursor = mContext.getContentResolver().query(
            LeaderContract.LeaderEntry.CONTENT_URI,
            null,
            LeaderContract.LeaderEntry._ID + " = " + leaderRowId,
            null,
            null
    );

    TestUtilities.validateCursor("testUpdateLeader.  Error validating leader entry update.",
            cursor, updatedValues);

    cursor.close();
}
 
Example 19
Project: MovieGuide   File: TestProvider.java   Source Code and License Vote up 4 votes
@Test
public void testUpdateFavoriteMovie() {
    Context appContext = InstrumentationRegistry.getTargetContext();
    // Create a new map of values, where column names are the keys
    ContentValues values = TestUtilities.createTheWallValues();

    Uri quoteUri = appContext.getContentResolver().
            insert(FavoritesContract.FavoriteColumns.uriMovie, values);
    long rowId = ContentUris.parseId(quoteUri);

    // Verify we got a row back.
    assertTrue(rowId != -1);
    Log.d(LOG_TAG, "New row id: " + rowId);

    ContentValues updatedValues = new ContentValues(values);
    updatedValues.put(FavoritesContract.FavoriteColumns._ID, rowId);
    updatedValues.put(FavoritesContract.FavoriteColumns.COLUMN_VOTE_COUNT, 250);

    // Create a cursor with observer to make sure that the content provider is notifying
    // the observers as expected
    Cursor locationCursor = appContext.getContentResolver().query(
            FavoritesContract.FavoriteColumns.uriMovie, null, null, null, null);

    TestUtilities.TestContentObserver tco = TestUtilities.getTestContentObserver();
    locationCursor.registerContentObserver(tco);

    int count = appContext.getContentResolver().update(
            FavoritesContract.FavoriteColumns.uriMovie, updatedValues,
            FavoritesContract.FavoriteColumns._ID + "= ?", new String[] { Long.toString(rowId)});
    assertEquals(count, 1);

    // Test to make sure our observer is called.  If not, we throw an assertion.
    // If your code is failing here, it means that your content provider
    // isn't calling getContext().getContentResolver().notifyChange(uri, null);
    tco.waitForNotificationOrFail();

    locationCursor.unregisterContentObserver(tco);
    locationCursor.close();

    // A cursor is your primary interface to the query results.
    Cursor cursor = appContext.getContentResolver().query(
            FavoritesContract.FavoriteColumns.uriMovie,
            null,   // projection
            FavoritesContract.FavoriteColumns._ID + " = " + rowId,
            null,   // Values for the "where" clause
            null    // sort order
    );

    TestUtilities.validateCursor("testUpdateFavoriteMovie.  Error validating quote entry update.",
            cursor, updatedValues);
    cursor.close();
}
 
Example 20
Project: Udacity_Sunshine   File: TestProvider.java   Source Code and License Vote up 4 votes
public void testUpdateLocation() {
    // Create a new map of values, where column names are the keys
    ContentValues values = TestUtilities.createNorthPoleLocationValues();

    Uri locationUri = mContext.getContentResolver().
            insert(LocationEntry.CONTENT_URI, values);
    long locationRowId = ContentUris.parseId(locationUri);

    // Verify we got a row back.
    assertTrue(locationRowId != -1);
    Log.d(LOG_TAG, "New row id: " + locationRowId);

    ContentValues updatedValues = new ContentValues(values);
    updatedValues.put(LocationEntry._ID, locationRowId);
    updatedValues.put(LocationEntry.COLUMN_CITY_NAME, "Santa's Village");

    // Create a cursor with observer to make sure that the content provider is notifying
    // the observers as expected
    Cursor locationCursor = mContext.getContentResolver().query(LocationEntry.CONTENT_URI, null, null, null, null);

    TestUtilities.TestContentObserver tco = TestUtilities.getTestContentObserver();
    locationCursor.registerContentObserver(tco);

    int count = mContext.getContentResolver().update(
            LocationEntry.CONTENT_URI, updatedValues, LocationEntry._ID + "= ?",
            new String[] { Long.toString(locationRowId)});
    assertEquals(count, 1);

    // Test to make sure our observer is called.  If not, we throw an assertion.
    //
    // Students: If your code is failing here, it means that your content provider
    // isn't calling getContext().getContentResolver().notifyChange(uri, null);
    tco.waitForNotificationOrFail();

    locationCursor.unregisterContentObserver(tco);
    locationCursor.close();

    // A cursor is your primary interface to the query results.
    Cursor cursor = mContext.getContentResolver().query(
            LocationEntry.CONTENT_URI,
            null,   // projection
            LocationEntry._ID + " = " + locationRowId,
            null,   // Values for the "where" clause
            null    // sort order
    );

    TestUtilities.validateCursor("testUpdateLocation.  Error validating location entry update.",
            cursor, updatedValues);

    cursor.close();
}