android - unsupported uri when accessing content provider -
edit java.lang.illegalargumentexception: unsupported uri: content://com.example.locationreminder.remindercontentprovider/items
shouldn't able pick whatever name want authority string? content uri?
i have following code:
public class remindercontentprovider extends contentprovider { private databasehelper mdbhelper; public static final string authority = "com.example.locationreminder.remindercontentprovider"; public static final uri content_uri = uri.parse("content://" + authority + "/items"); public static final string table_name = "reminder"; private static final int allrows = 1; private static final int single_row = 2; public static interface remindercolumns extends basecolumns { public static final uri content_uri = remindercontentprovider.content_uri; public static final string title = "title"; public static final string date = "date"; public static final string content_path = "items"; public static final string content_type = contentresolver.cursor_dir_base_type + "/vnd.locationreminder.items"; public static final string content_item_type = contentresolver.cursor_item_base_type + "/vnd.locationreminder.items"; } static { surimatcher = new urimatcher(urimatcher.no_match); surimatcher.adduri(authority, remindercolumns.content_path, allrows); surimatcher.adduri(authority, remindercolumns.content_path + "/#", single_row); } @override public boolean oncreate() { mdbhelper = new databasehelper(getcontext()); return true; } @override public string gettype(uri uri) { switch(surimatcher.match(uri)) { case allrows: return remindercolumns.content_type; case single_row: return remindercolumns.content_item_type; default: throw new illegalargumentexception("unsupported uri: " + uri); } } @override public cursor query(uri uri, string[] projection, string selection, string[] selectionargs, string sortorder) { sqlitequerybuilder builder = new sqlitequerybuilder(); builder.settables(databasehelper.reminder_table_name); switch (surimatcher.match(uri)) { case allrows: // nice , break; case single_row: // limit query 1 row @ most: builder.appendwhere(remindercolumns._id + " = " + uri.getlastpathsegment()); break; default: throw new illegalargumentexception("unsupported uri: " + uri); } cursor cursor = builder.query(mdbhelper.getwritabledatabase(), projection, selection, selectionargs, null, null, sortorder); // if want notified of changes: cursor.setnotificationuri(getcontext().getcontentresolver(), uri); return cursor; } note: implemented insert(), update() , delete() aren't relevant error get.
the manifest file:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.locationreminder" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="15" android:targetsdkversion="17" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <provider android:name="com.example.locationreminder.remindercontentprovider" android:authorities="com.example.locationreminder.remindercontentprovider" android:singleuser="false" /> <!-- other users can't use provider --> <activity android:name="com.example.locationreminder.mainactivity" android:theme="@android:style/theme.holo.light" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name="com.example.locationreminder.newreminderactivity" android:label="@string/title_activity_new_reminder" android:theme="@style/calendartheme.withactionbar" > </activity> </application> edit
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); settheme(android.r.style.theme_holo_light); setcontentview(r.layout.activity_main); cursor cursor = managedquery(remindercontentprovider.content_uri, null, null, null, null); msimplecursoradapter = new specialadapter(this, r.layout.row, cursor, new string[]{ databasehelper.key_id, databasehelper.key_title, databasehelper.key_date } new int[] { r.id.titleid, r.id.datetimeorlocationid }, cursoradapter.flag_register_content_observer); getloadermanager().initloader(0, null, this); listview listview = (listview) findviewbyid(r.id.list); listview.setadapter(msimplecursoradapter); }
does error happen when switch (surimatcher.match(uri)) falls default case? if so, need pass uri matches allrows or single_row
Comments
Post a Comment