Skip to content

Commit

Permalink
1.16 Implement ContentProvider getType() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Beginning Android committed Aug 23, 2016
1 parent 81fe10a commit a244b5a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
13 changes: 13 additions & 0 deletions app/src/main/java/com/example/android/pets/data/PetContract.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.example.android.pets.data;

import android.net.Uri;
import android.content.ContentResolver;
import android.provider.BaseColumns;

/**
Expand Down Expand Up @@ -58,6 +59,18 @@ public static final class PetEntry implements BaseColumns {
/** The content URI to access the pet data in the provider */
public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_PETS);

/**
* The MIME type of the {@link #CONTENT_URI} for a list of pets.
*/
public static final String CONTENT_LIST_TYPE =
ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_PETS;

/**
* The MIME type of the {@link #CONTENT_URI} for a single pet.
*/
public static final String CONTENT_ITEM_TYPE =
ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_PETS;

/** Name of database table for pets */
public final static String TABLE_NAME = "pets";

Expand Down
18 changes: 13 additions & 5 deletions app/src/main/java/com/example/android/pets/data/PetProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,6 @@ public Cursor query(Uri uri, String[] projection, String selection, String[] sel
return cursor;
}

@Override
public String getType(Uri uri) {
return null;
}

@Override
public Uri insert(Uri uri, ContentValues contentValues) {
final int match = sUriMatcher.match(uri);
Expand Down Expand Up @@ -261,4 +256,17 @@ public int delete(Uri uri, String selection, String[] selectionArgs) {
throw new IllegalArgumentException("Deletion is not supported for " + uri);
}
}

@Override
public String getType(Uri uri) {
final int match = sUriMatcher.match(uri);

This comment has been minimized.

Copy link
@tanmaymishra2

tanmaymishra2 Jan 4, 2020

One thing I don't get is why every match variable is defined as a final variable?

This comment has been minimized.

Copy link
@hanuz06

hanuz06 Aug 23, 2021

I think, if it is final, the variable is a constant(cannot be changed).

switch (match) {
case PETS:
return PetEntry.CONTENT_LIST_TYPE;
case PET_ID:
return PetEntry.CONTENT_ITEM_TYPE;
default:
throw new IllegalStateException("Unknown URI " + uri + " with match " + match);
}
}
}

6 comments on commit a244b5a

@vibhakarsaraswat
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the "public String getType(Uri uri)" method-->

  • it should be "case PETS_ID:"
  • and not "case PET_ID:"

@kowa1ski
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think PET_ID is right. Are you sure?

@schv09
Copy link

@schv09 schv09 commented on a244b5a Jun 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends on how you defined the constants, it's correct as to how the course defined them.

@owolabiezekiel
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It all depends on what you have been using from the beginning. I have been using PET_ID because I thought since it was gonna be a single pet, there was no need to pluralize the name. you are welcome to decide otherwise

@Nikoloutsos
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the value of implementing this method, although I've read some explanations from the StackOverflow.

@shanip89
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's keeps give me this error:

Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)

who knows about that? :\

Please sign in to comment.