编辑
我得到一个java.lang.IllegalArgumentException: Unsupported URI: content://com.example.locationreminder.remindercontentprovider/items
我不应该能够为 AUTHORITY 字符串选择我想要的任何名称吗?以及内容URI?
我有以下代码:
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:
// all nice and well
break;
case SINGLE_ROW:
// limit query to one row at 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 we want to be notified of any changes:
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
注意:我还实现了与我得到的错误无关的insert()、update() 和 delete() 。
清单文件:
<?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 this 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>
编辑
@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);
}