Skip to content

Commit

Permalink
1.8 Add constants for pet content URIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Beginning Android committed Aug 23, 2016
1 parent 7d0e356 commit 07f3866
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 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 @@ -15,6 +15,7 @@
*/
package com.example.android.pets.data;

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

/**
Expand All @@ -26,12 +27,37 @@ public final class PetContract {
// give it an empty constructor.
private PetContract() {}

/**
* The "Content authority" is a name for the entire content provider, similar to the
* relationship between a domain name and its website. A convenient string to use for the
* content authority is the package name for the app, which is guaranteed to be unique on the
* device.
*/
public static final String CONTENT_AUTHORITY = "com.example.android.pets";

/**
* Use CONTENT_AUTHORITY to create the base of all URI's which apps will use to contact
* the content provider.
*/
public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);

/**
* Possible path (appended to base content URI for possible URI's)
* For instance, content://com.example.android.pets/pets/ is a valid path for
* looking at pet data. content://com.example.android.pets/staff/ will fail,
* as the ContentProvider hasn't been given any information on what to do with "staff".
*/
public static final String PATH_PETS = "pets";

/**
* Inner class that defines constant values for the pets database table.
* Each entry in the table represents a single pet.
*/
public static final class PetEntry implements BaseColumns {

This comment has been minimized.

Copy link
@mahmoudhamdy123

mahmoudhamdy123 Jun 21, 2019

//Next, we concatenate the CONTENT_AUTHORITY constant with the scheme “content://” we will create
// the BASE_CONTENT_URI which will be shared by every URI associated with PetContract:
public static final String CONTENT_AUTHORITY = "com.example.android.pets";
//To make this a usable URI, we use the parse method which takes in a URI string and returns a Uri.
public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
//This constants stores the path for each of the tables which will be appended to the base content URI.
public static final String PATH_PETS = "pets";
//Complete CONTENT_URI
public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_PETS);

This comment has been minimized.

Copy link
@MinusRF

MinusRF Jan 29, 2021

Great

/** The content URI to access the pet data in the provider */
public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_PETS);

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

Expand Down

22 comments on commit 07f3866

@bsalsth
Copy link

@bsalsth bsalsth commented on 07f3866 Dec 5, 2016

Choose a reason for hiding this comment

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

ERROR: Inner class cant have static declaration

LINE 59:
public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_PETS);

@kharak
Copy link

@kharak kharak commented on 07f3866 Dec 6, 2016

Choose a reason for hiding this comment

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

indeed its working fine here. can you show some code.

@Abdulkadir98
Copy link

Choose a reason for hiding this comment

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

I have the same error: Here's the code
public class PetsEntry implements BaseColumns {

    public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT,PATH_PETS);

    public static final String TABLE_NAME = "Pets";
    public static final String COLUMN_ID = BaseColumns._ID;
     public static final String COLUMN_PET_NAME = "name";
     public static final String COLUMN_PET_BREED = "breed";
     public static final String COLUMN_PET_GENDER = "gender";
     public static final String COLUMN_PET_WEIGHT = "weight";
     public static final int GENDER_MALE = 1;
     public static final int GENDER_FEMALE = 2;
     public static final int GENDER_UNKNOWN = 0;
}

@Abdulkadir98
Copy link

Choose a reason for hiding this comment

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

error resolved 👍 you simply have to declare Petentry class as static.

@dfdazac
Copy link

@dfdazac dfdazac commented on 07f3866 Jan 27, 2017

Choose a reason for hiding this comment

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

I don't feel quite comfortable with two different constants for the pets table name, namely
public static final String PATH_PETS = "pets"; and
public static final String TABLE_NAME = "pets";

Is there any particular reason for this?

@Devenc234
Copy link

Choose a reason for hiding this comment

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

@dfdazac you may have two names(you actual name and a nickname) but both of used to call you at different places and with different references.

@Amr-Y
Copy link

@Amr-Y Amr-Y commented on 07f3866 Jul 5, 2017

Choose a reason for hiding this comment

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

@Devenc234 @dfdazac correct me if I'm wrong but you can't have the URI contain a name different than your database table name.
so both strings are and must be the same which in turn makes no sense why they are 2 and not 1
it should be like this:
public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, TABLE_NAME);

@ShahoodulHassan
Copy link

Choose a reason for hiding this comment

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

@Devenc234 I have the same argument as given by @Amr-Y above. U cant have a real and a pet name for a table in this case. PATH_PETS and TABLE_NAME have to be the same, so why two separate variables?

@arunalexanderkk
Copy link

Choose a reason for hiding this comment

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

@Devenc234 is right, we all have like that

@albertoastroc
Copy link

Choose a reason for hiding this comment

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

Is there a significant reason why the first 3 constants are placed outside of the PetEntry class?

@Rtchaik
Copy link

@Rtchaik Rtchaik commented on 07f3866 Feb 3, 2018

Choose a reason for hiding this comment

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

@albertoastroc, because there could be more than 1 table and you or other developers will clearly see in the future that those constants are general and available for use .

@sherifhisham
Copy link

Choose a reason for hiding this comment

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

thank GOD no errors currently

@Babadzhanov
Copy link

Choose a reason for hiding this comment

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

I was thinking the same way.. why 2 constants with same value?? We will probably find out later 🤣

@Bong79
Copy link

@Bong79 Bong79 commented on 07f3866 Sep 11, 2018

Choose a reason for hiding this comment

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

CONTENT_URI is never used, why? (Im on lesson 4.16)

@xht418
Copy link

@xht418 xht418 commented on 07f3866 Nov 23, 2018

Choose a reason for hiding this comment

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

What's the difference between PATH_PETS (line 50) and TABLE_NAME (line 62)? I mean, their names are different obviously, but the values are the same, they are all pointing to the table which is also the name of the path.

@BasemSaabneh
Copy link

Choose a reason for hiding this comment

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

Nice Work

@AndriiBibik
Copy link

Choose a reason for hiding this comment

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

I have a bit diff app. Anyways..
isn't that better to put CONTENT_URI like this:
public final static Uri CONTENT_URI_PETS = Uri.withAppendedPath(BASE_CONTENT_URI, TABLE_NAME);
instead of:
public final static Uri CONTENT_URI_PRACTITIONERS = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_PETS);
this way we dont have add additional constant PATH_PETS .

@appfactoryCo
Copy link

Choose a reason for hiding this comment

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

Where is the Uri for a single pet query?

@xht418
Copy link

@xht418 xht418 commented on 07f3866 Feb 19, 2019

Choose a reason for hiding this comment

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

@appfactoryCo see line 59, single pet query would be PetEntry.CONTENT_URI + rowId.

@getsadzeg
Copy link

Choose a reason for hiding this comment

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

Is there a significant reason why the first 3 constants are placed outside of the PetEntry class?

@albertoastroc Because they don't really have a connection to a particular entry, i.e. table, but the database(i.e. outer Contract class) instead.

@Awais-Mansha
Copy link

Choose a reason for hiding this comment

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

@appfactoryCo see line 59, single pet query would be PetEntry.CONTENT_URI + rowId.
YES you are right

@Bong79
Copy link

@Bong79 Bong79 commented on 07f3866 Jun 7, 2019 via email

Choose a reason for hiding this comment

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

Please sign in to comment.