我一直在插入一条记录,在此期间我使用以下代码
解析返回的id Uri movieUri = mContext.getContentResolver()
.insert(MoviesContract.MovieEntry.CONTENT_URI, contentValues);
long movieRowId = ContentUris.parseId(movieUri);
当我查询数据库并转储光标时,显示的_ID字段值不同。
请解释一下,如果这两个ID被区别对待或者它们是相同的。
以下是内容提供商代码
public class MoviesProvider extends ContentProvider{
private static final UriMatcher uriMatcher = buildUriMatcher();
private MoviesDbHelper moviesDbHelper;
static final int ALLMOVIES = 100;
static final int SPECIFICMOVIE = 101;
static UriMatcher buildUriMatcher() {
final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = MoviesContract.CONTENT_AUTHORITY;
uriMatcher.addURI(authority, MoviesContract.PATH_MOVIES, ALLMOVIES);
uriMatcher.addURI(authority, MoviesContract.PATH_MOVIES + "/#", SPECIFICMOVIE);
return uriMatcher;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
final SQLiteDatabase db = moviesDbHelper.getWritableDatabase();
final int match = uriMatcher.match(uri);
int rowsUpdated;
switch(match) {
case ALLMOVIES:
rowsUpdated = db.update(MoviesContract.MovieEntry.TABLE_NAME,values,selection,selectionArgs);
break;
default:
throw new UnsupportedOperationException("Unknown Uri: " + uri);
}
if (rowsUpdated != 0) {
getContext().getContentResolver().notifyChange(uri,null);
}
return rowsUpdated;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
final SQLiteDatabase db = moviesDbHelper.getWritableDatabase();
final int match = uriMatcher.match(uri);
Uri returnUri;
switch(match) {
case ALLMOVIES:
long _id = db.insert(MoviesContract.MovieEntry.TABLE_NAME, null, values);
if (_id > 0) {
returnUri = MoviesContract.MovieEntry.buildMovieUri(id);
}
else {
throw new android.database.SQLException("Failed to insert row into " + uri);
}
break;
default:
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return returnUri;
}
MovieContract.java
public class MoviesContract {
public static final String CONTENT_AUTHORITY = "com.example.android.moviesapp";
public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
public static final String PATH_MOVIES = "movies";
public static final class MovieEntry implements BaseColumns {
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon().appendEncodedPath(PATH_MOVIES).build();
public static final String CONTENT_DIR_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_MOVIES;
public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_MOVIES;
public static final String TABLE_NAME = "movie";
public static final String COLUMN_MOVIE_TITLE = "original_title";
public static final String COLUMN_POSTERPATH = "poster_path";
public static final String COLUMN_SYNOPSIS = "overview";
public static final String COLUMN_RELEASEDATE = "release_date";
public static final String COLUMN_VOTECOUNT = "vote_count";
public static final String COLUMN_VOTEAVERAGE = "vote_average";
public static Uri buildMovieUri(long id) {
return ContentUris.withAppendedId(CONTENT_URI,id);
}
}
}