I've implemented my custom ContentProvider
and it has several URI
s:
The main one:
//Return all items, uriType = ALLITEMS
String BASEURI = "content://authority/items"
and
//Return all items in category #, uriType = ITEMS
"content://authority/items/cat/#"
//Return all items in category # starting with *, uriType = ITEMS_INITIAL
"content://authority/items/cat/#/*"
My Activity
implements these Loader
callbacks:
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle arg1) {
CursorLoader mCursorLoader = null;
switch (id) {
case 0:
mCursorLoader = new CursorLoader(
mActivity,
Uri.parse("content://authority/items/cat/"
+mCurrentID), mColumns, null, null, null);
break;
}
return mCursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
switch (loader.getId()) {
case 0:
cursor.setNotificationUri(getContentResolver(),MyContentProvider.BASEURI);
if (null == mAdapter)
mAdapter = new GridViewCursorAdapter(this, cursor,0);
//gv is a GridView
if (gv.getAdapter() != mAdapter)
gv.setAdapter(mAdapter);
if (mAdapter.getCursor() != cursor)
mAdapter.swapCursor(cursor);
break;
}
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
mAdapter.swapCursor(null);
}
When I want to insert data I use:
for (ItemClass item : itemsToInsert) {
getContentResolver().insert(MyContentProvider.BASEURI, itemToContentValues(item));
}
And finally insert
method in MyContentProvider
is so defined:
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase database = db.getWritableDatabase();
int turiType = sURIMatcher.match(uri);
long id = 0;
switch (uriType) {
case ALLITEMS:
id = database.insert(MySQLiteHelper.TABLE_ITEMS, null, values);
break;
default:
throw new IllegalArgumentException("Unknown URI (" + uri + ")");
}
getContext().getContentResolver().notifyChange(uri, null);
return null;//I will implement uri path to single item later
}
As you can see, the default URI used to initialize the Loader
is the filter by category id and not the BASEURI
one, but I used Cursor.setNotificationUri
to set the notification URI to BASEURI
, but the content of my GridView
isn't updated.
If I restart the Activity
I can see the inserted data, so it's just the notification that doesn't work. What should I change to get the loader notified properly?