why do we need to set a projectionMap with SQLiteQueryBuilder ? For example, we set this HashMap :
sNotesProjectionMap = new HashMap<String, String>();
sNotesProjectionMap.put(Notes._ID, Notes._ID);
sNotesProjectionMap.put(Notes.TITLE, Notes.TITLE);
sNotesProjectionMap.put(Notes.NOTE, Notes.NOTE);
sNotesProjectionMap.put(Notes.CREATED_DATE, Notes.CREATED_DATE);
sNotesProjectionMap.put(Notes.MODIFIED_DATE, Notes.MODIFIED_DATE);
and in the “query”, we also specify the projection, selection etc. from the Activity, so what the point to do this hashmap first?
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(NOTES_TABLE_NAME);
switch (sUriMatcher.match(uri)) {
case NOTES:
qb.setProjectionMap(sNotesProjectionMap);
break;
case NOTE_ID:
qb.setProjectionMap(sNotesProjectionMap);
qb.appendWhere(Notes._ID + "=" + uri.getPathSegments().get(1));
break;
//...
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
Thanks
You can check the documentation for setprojectionmap . It does translations from application column names to database columns. Hashmap will have the translation.
The key in the map is the column name the application will use and the value is the column name in the database.
They are identical in the NotePadProvider in your example, but they could differ if the name of a column in the database changed and you wanted to keep the change transparent to applications using the content provider.
For example, if the database column name of the “title” field (which is stored in Notes.TITLE) changed to “content”, a new constant Notes.CONTENT could be created and the projection insertion would look like: