app/src/main/java/com/example/android/pets/Data/PetProvider.java
/**
     * Perform the query for the given URI. Use the given projection, selection, selection arguments, and sort order.
     */
Perform the query for the given URI. Use the given projection, selection, selection arguments, and sort order.
Perform the query for the given URI . Use the given projection selection selection arguments and sort order .
@Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
                        String sortOrder) {
        SQLiteDatabase database = mDbHelper.getReadableDatabase();
        Cursor cursor;
        int match = sUriMatcher.match(uri);
        switch (match) {
            case PETS:
                cursor = database.query(PetContract.PetEntry.TABLE_NAME,
                        projection,
                        selection,
                        selectionArgs,
                        null,
                        null,
                        sortOrder);
                break;
            case PET_ID:
                 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 = PetContract.PetEntry._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(PetContract.PetEntry.TABLE_NAME, projection, selection, selectionArgs,
                        null, null, sortOrder);
                break;
            default:
                throw new IllegalArgumentException("Cannot query unknown URI " + uri);
        }
         Set notification URI on the Cursor,
         so we know what content URI the Cursor was created for.
         If the data at this URI changes, then we know we need to update the Cursor.
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;
    }
@ Override public Cursor query ( Uri uri , String [ ] projection , String selection , String [ ] selectionArgs , String sortOrder ) { SQLiteDatabase database = mDbHelper . getReadableDatabase ( ) ; Cursor cursor ; int match = sUriMatcher . match ( uri ) ; switch ( match ) { case PETS : cursor = database . query ( PetContract . PetEntry . TABLE_NAME , projection , selection , selectionArgs , null , null , sortOrder ) ; break ; case PET_ID : selection = PetContract . PetEntry . _ID + "=?" ; selectionArgs = new String [ ] { String . valueOf ( ContentUris . parseId ( uri ) ) } ; cursor = database . query ( PetContract . PetEntry . TABLE_NAME , projection , selection , selectionArgs , null , null , sortOrder ) ; break ; default : throw new IllegalArgumentException ( "Cannot query unknown URI " + uri ) ; } cursor . setNotificationUri ( getContext ( ) . getContentResolver ( ) , uri ) ; return cursor ; }
Perform the query for the given URI.
Perform the query for the given URI .
// Get readable database // This cursor will hold the result of the query // Figure out if the URI matcher can match the URI to a specific 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. // This will perform a query on the pets table where the _id equals 3 to return a // Cursor containing that row of the table. // Set notification URI on the Cursor, // so we know what content URI the Cursor was created for. // If the data at this URI changes, then we know we need to update the Cursor.