0
编辑 我得到一个java.lang.IllegalArgumentException: Unsupported URI: content://com.example.locationreminder.remindercontentprovider/items
不支持的URI时,内容提供商
应该不是我能够挑选我想管理局串什么名字?以及内容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;
}
注:我还实施插入(),更新()和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);
}
是的,但我该怎么做?我认为内容解析器在我调用时调用它:getLoaderManager()。initLoader(0,null,this);查询()切换语句中的URI是内容://com.example.locationreminder.remindercontentprovider/items – user2246120 2013-05-10 04:31:30
那么为什么异常说内容://com.example.locationreminder.remindercontentprovider(不包括/项后缀)?看起来你没有在创建Loader – Y2i 2013-05-10 05:56:25
时附加“/ items”!我编辑了这篇文章:它将“/ items”附加到查询()中。但是现在我得到另一个异常:IllegalArgumentException:找不到列'_id'。我从MainActivity添加了onCreate方法。查询方法中的投影String []仅具有标题,日期字段。那从哪里来的? – user2246120 2013-05-11 16:58:30