我似乎在代码的某处遇到了一个小错误,它让我感到沮丧,因为我似乎找不到它。
无论如何,插入方法以及更新方法都可以正常工作。 这是我用于删除方法的代码:
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Get writeable database
SQLiteDatabase database = mDbHelper.getWritableDatabase();
final int match = sUriMatcher.match(uri);
switch (match) {
case HISTORY:
// Delete all rows that match the selection and selection args
return database.delete(HistoryContract.HistoryEntry.TABLE_NAME, selection, selectionArgs);
case HISTORY_ID:
// Delete a single row given by the ID in the URI
selection = HistoryContract.HistoryEntry._ID + "=?";
selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
return database.delete(HistoryContract.HistoryEntry.TABLE_NAME, selection, selectionArgs);
default:
throw new IllegalArgumentException("Deletion is not supported for " + uri);
}
}
URI匹配器:
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
// Static initializer. This is run the first time anything is called from this class.
static {
// The calls to addURI() go here, for all of the content URI patterns that the provider
// should recognize. All paths added to the UriMatcher have a corresponding code to return
// when a match is found.
// TODO: Add 2 content URIs to URI matcher
sUriMatcher.addURI(HistoryContract.CONTENT_AUTHORITY,HistoryContract.PATH_HISTORY,HISTORY);
sUriMatcher.addURI(HistoryContract.CONTENT_AUTHORITY,HistoryContract.PATH_HISTORY + "/#",HISTORY_ID);
}
调用删除方法:
int rowsDeleted = getContentResolver().delete(mCurrentHistoryUri,null,null);
mCurrentHistoryUri
我通过以下方式传递给意图中的活动:
displayIntent.setData(currentHistoryUri);
它提供了正确的URI,当我在LOG行中写出Deleted时,我得到的数字是0。
通过cursorloader显示对象。
**** **** EDIT 我尝试使用select语句并检查歌曲名称以适合所显示的歌曲(因为我不想使用这可能会删除多个行),删除有效,任何想法?