Search code examples
androidmongodbsqliteuriandroid-contentprovider

Android content uri id as string


In my Android app i use SQLite with content provider. For some (backend) reasons, my tables have _id primary key as VARCHAR.

The problem is:

I need to use content uri with appended id. In all examples what i saw content uri with id looks as

content://<autority>/<model_or_table>/<long_id>

and adds to UriMatcher as

UriMatcher.add(autority, model_or_id + "/#", uri_id)

and of course thats "/#" matching id works only with digits.

My question is:

There is any way to use content uri with appended String id? Typical id is something like

50c11d774e5e1b99698b6879

and typical content uri must be as

content://com.my.app.autority/Users/50c11d774e5e1b99698b6879

and how to work with uri like this and UriMatcher?

I know that i can write selection directive (WHERE _id = "50c11d774e5e1b99698b6879") to query method, but i need exactly URI or way to project unique string ids to the unique long values in SQLiteDB.

There is any way? Thanks in adwance) Sorry for bad English)


Solution

  • Please refer to documentation about creating a Content Provider found here. Specifically, refer to heading Content URI patterns. As you correctly pointed out the # is a wildcard which will match single rows with an integer ID. The * wildcard will not match single rows with non-numeric IDs.

    As far as I can tell, the UriMatcher class cannot help you in this case.

    Please also refer to the UriMatcher Reference page here.