Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android OnLoadFinished() not called when cursor changes

I have created a simple Activity which implements the LoaderManager.LoaderCallbacks<Cursor> interface. The OnCreateLoader() is straightforward:

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args)
{
    Log.d(TAG, "On create loader");

    Uri queryUri = ContentUris.withAppendedId(SmartPresentationMessage.Person.CONTENT_URI, 1);
    CursorLoader cursorLoader = new CursorLoader(this, queryUri, null, null, null, null);
    return cursorLoader;
}

This method is called when I call getLoaderManager().initLoader(0, null, this) when the Activity is created.

My problem is that on my ContentProvider, which has the following query() method:

@Override
public Cursor query(Uri uri, String[] projection, String where,
        String[] whereArgs, String sortOrder)
{
    int match = sUriMatcher.match(uri);

    Cursor queryCursor;
    SQLiteDatabase mdb = mOpenHelper.getReadableDatabase();

    switch (match)
    {
        case PERSON:
            long personID = ContentUris.parseId(uri);

            queryCursor = mdb.query(TABLE_NAME, projection,
                        SmartPresentationMessage.Person._ID + " = " + personID,
                        whereArgs, null, null, sortOrder);

            asyncQueryRequest("" + (taskTag ++ ) , QUERY_URI + "/" + "person");

            return queryCursor;
        default:
            throw new IllegalArgumentException("unsupported uri: " + uri);
    }
}

the queryCursor gets updated in the database after a I get a response from a webserver. However, when I call the getContext().getContentResolver().notifyChange(uri, null) method in the ContentProvider's update method, my OnLoadFinished() method on the Activity is not called, even though the uri is identical to the one used when instantiating the CursorLoader.

This is the update method on the ContentProvider:

@Override
public int update(Uri uri, ContentValues values, String where, String[] whereArgs)
{
    //getContext().getContentResolver().notifyChange(uri, null);


    // insert the initialValues into a new database row
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    int affected;
    try
    {
        switch (sUriMatcher.match(uri))
        {
            case PERSON:
                long personID = ContentUris.parseId(uri);;
                affected = db.update(TABLE_NAME, values,
                        SmartPresentationMessage.Person._ID + " = " + personID,
                        whereArgs);
                getContext().getContentResolver().notifyChange(uri, null);

                break;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
    }
    finally
    {
        db.close();
    }

    return affected;
}

Can somebody tell me what could be the cause of my OnLoadFinshed() method not being called when calling notifyChange() on the same uri?

Thank you, Cristi

like image 354
chriss Avatar asked Dec 28 '22 00:12

chriss


1 Answers

Before returning your Cursor in your provider's query() method you'll need to call the following method

cursor.setNotificationUri(contentResolver, uri); 

You can get the ContentResolver via a Context. In a ContentProvider there's a method getContext() for this.

like image 52
novak Avatar answered Jan 30 '23 07:01

novak