Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Increment DB field via ContentValues

I'm updating an item in a ListView via the getContentResolver().update() method, and I'd like to increment a 'views' field via a ContentValue, but can't figure out if this is possible.

I could do this with raw SQL SET views = views + 1, but setting a ContentValue, such as cv.put("views", "views + 1") results in the views field being set explicitly to "views + 1" rather than a number.

Any pointers on this on, or am I left to a more manual approach?

Thanks,

Paul

UPDATE:

I've gone back to using raw SQL to perform the update for now, and then manually notify the underlying CursorAdapter of the change via getContentResolver().notifyChange(). Would still be great if I could figure out a way to do this directly via getContentResolver().update(), so if anyone has a way to do that, please post it here.

like image 823
Unpossible Avatar asked Mar 28 '11 21:03

Unpossible


1 Answers

My final solution to this was to use a custom Uri (along the lines of ...item/5/updateviews) via the ContentProvider such that a call to getContentResolver().update(customUri, ...) passing this Uri tells my ContentProvider's overridden update() method to increment the view count for this item, thus avoiding the need to pass values to update() via it's ContentValues parameter.

like image 176
Unpossible Avatar answered Oct 31 '22 15:10

Unpossible