Created
September 15, 2018 19:15
-
-
Save barmgeat/33394e96c71f4e4111d62a938fbb2b69 to your computer and use it in GitHub Desktop.
Implemt the query() Method SQLit Content Provider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, | |
String sortOrder) { | |
// Get readable database | |
SQLiteDatabase database = mDbHelper.getReadableDatabase(); | |
// This cursor will hold the result of the query | |
Cursor cursor=null; | |
// Figure out if the URI matcher can match the URI to a specific code | |
int match = uriMatcher.match(uri); | |
switch (match) { | |
case WORDS_CODE: | |
// For the PETS code, query the pets table directly with the given | |
// projection, selection, selection arguments, and sort order. The cursor | |
// could contain multiple rows of the pets table. | |
// TODO: Perform database query on pets table | |
break; | |
case ID_WORD_CODE: | |
// For the PET_ID code, extract out the ID from the URI. | |
// For an example URI such as "content://com.example.android.pets/pets/3", | |
// the selection will be "_id=?" and the selection argument will be a | |
// String array containing the actual ID of 3 in this case. | |
// | |
// For every "?" in the selection, we need to have an element in the selection | |
// arguments that will fill in the "?". Since we have 1 question mark in the | |
// selection, we have 1 String in the selection arguments' String array. | |
selection = Words._ID + "=?"; | |
selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) }; | |
// This will perform a query on the pets table where the _id equals 3 to return a | |
// Cursor containing that row of the table. | |
cursor = database.query(Words.TABLE_NAME, projection, selection, selectionArgs, | |
null, null, sortOrder); | |
break; | |
default: | |
throw new IllegalArgumentException("Cannot query unknown URI " + uri); | |
} | |
return cursor; | |
} | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment