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

The following are Jave code examples for showing how to use unregisterContentObserver() 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: AccountsLoader.java   Source Code and License Vote up 5 votes
/**
 * Helper function to take care of releasing resources associated with an
 * actively loaded data set.
 */
protected void onReleaseResources(Cursor c) {
    if(c != null) {
        c.unregisterContentObserver(loaderObserver);
        c.close();
    }
    if(loadStatus) {
        getContext().getContentResolver().unregisterContentObserver(loaderObserver);
    }
}
 
Example 2
Project: CSipSimple   File: FavLoader.java   Source Code and License Vote up 5 votes
/**
 * Helper function to take care of releasing resources associated with an
 * actively loaded data set.
 */
protected void onReleaseResources(Cursor c) {
    if(c != null) {
        c.unregisterContentObserver(loaderObserver);
        c.close();
    }

    getContext().getContentResolver().unregisterContentObserver(loaderObserver);
}
 
Example 3
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 4
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 5
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 6
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 7
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 8
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 9
Project: q-mail   File: MergeCursor.java   Source Code and License Vote up 4 votes
@Override
public void unregisterContentObserver(ContentObserver observer) {
    for (Cursor cursor : mCursors) {
        cursor.unregisterContentObserver(observer);
    }
}
 
Example 10
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 11
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 12
Project: MovieGuide   File: TestProvider.java   Source Code and License Vote up 4 votes
@Test
public void testUpdateFavoriteTVShow() {
    Context appContext = InstrumentationRegistry.getTargetContext();
    // Create a new map of values, where column names are the keys
    ContentValues values = TestUtilities.createTheSimpsonsValues();

    Uri quoteUri = appContext.getContentResolver().
            insert(FavoritesContract.FavoriteColumns.uriTVShow, 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, 500);

    // 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.uriTVShow, null, null, null, null);

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

    int count = appContext.getContentResolver().update(
            FavoritesContract.FavoriteColumns.uriTVShow, 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.uriTVShow,
            null,   // projection
            FavoritesContract.FavoriteColumns._ID + " = " + rowId,
            null,   // Values for the "where" clause
            null    // sort order
    );

    TestUtilities.validateCursor("testUpdateFavoriteTVShow.  Error validating quote entry update.",
            cursor, updatedValues);
    cursor.close();
}
 
Example 13
Project: MovieGuide   File: TestProvider.java   Source Code and License Vote up 4 votes
@Test
public void testUpdateFavoritePerson() {
    Context appContext = InstrumentationRegistry.getTargetContext();
    // Create a new map of values, where column names are the keys
    ContentValues values = TestUtilities.createMargotRobbieValues();

    Uri quoteUri = appContext.getContentResolver().
            insert(FavoritesContract.FavoriteColumns.uriPerson, 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_KNOWNFOR_POSTER_PATH,
            "/rP36Rx5RQh0rmH2ynEIaG8DxbV2.jpg");

    // 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.uriPerson, null, null, null, null);

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

    int count = appContext.getContentResolver().update(
            FavoritesContract.FavoriteColumns.uriPerson, 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.uriPerson,
            null,   // projection
            FavoritesContract.FavoriteColumns._ID + " = " + rowId,
            null,   // Values for the "where" clause
            null    // sort order
    );

    TestUtilities.validateCursor("testUpdateFavoritePerson.  Error validating quote entry update.",
            cursor, updatedValues);
    cursor.close();
}
 
Example 14
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();
}