Reputation: 7436
I have worked with ContentProviders in previous projects and this has always worked for me however I can't get my head around the problem this time. When I run a update() using a Content Resolver it duplicates the object in the database instead of updating it.
Here is the code the fires the update. It only includes the required updated data in the ContentValues not a whole repeat of the object. The index variable in the where statement is the row number of the object in the database, this index is correct.
ContentValues cv = new ContentValues();
cv.put(MessageProvider.KEY_MESSAGE_STATUS, m.getStatus());
cr.update(MessageProvider.CONTENT_URI_MESSAGE, cv, MessageProvider.KEY_ID + "=" + index, null);
I do not believe this code is at fault as I have tried changing this and it still produces the same duplication. My alternative was to include the row index in the Uri.
This is the code from the Content Provider.
@Override
public int update(Uri uri, ContentValues values, String where,
String[] whereArgs) {
int count = 0;
String whereString;
switch (uriMatcher.match(uri)) {
case MESSAGE:
count = messageDB.update(MESSAGE_TABLE, values, where, whereArgs);
break;
case MESSAGE_ID_:
String segment = uri.getPathSegments().get(1);
if (!TextUtils.isEmpty(where)) {
whereString = " AND (" + where + ')';
} else {
whereString = "";
}
count = messageDB.update(MESSAGE_TABLE, values, KEY_ID + "="
+ segment + whereString, whereArgs);
break;
default: throw new IllegalArgumentException("Unsupported URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
Nothing jumps out at me here as being incorrect but I'm sure more expert eyes will be able to help! Many thanks!
Upvotes: 1
Views: 1247
Reputation: 7436
Thanks for the responses, it's much appreciated!
After digging deeper into the code and debugging it appears the code about is fine, as I already said I thought it way. The problem turned out to be nothing todo with the content provider, it was an error with the server back-end including erroneous data in responses to the application.
Thanks for posting ideas. Lesson learned for me: don't leave the debugger until you have a solution, even if it does take a few days...
Upvotes: 1
Reputation: 1061
Thing that comes to the top of my mind is.
Have you verified that the where clause is correct and that it points to a record that actually exists.
And how does the messageDB.update handle when you have a where clause that actually doesn't exist?
Haven't worked with ContentProviders myself. But that seems to be a possible error-source.
Upvotes: 1