Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson three #4

Open
wants to merge 10 commits into
base: lesson-two
Choose a base branch
from
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".CatalogActivity" />
</activity>
<provider
android:name=".data.PetProvider"
android:authorities="com.example.android.pets"
android:exported="false" />
</application>

</manifest>
46 changes: 14 additions & 32 deletions app/src/main/java/com/example/android/pets/CatalogActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
Expand All @@ -28,16 +28,12 @@
import android.widget.TextView;

import com.example.android.pets.data.PetContract.PetEntry;
import com.example.android.pets.data.PetDbHelper;

/**
* Displays list of pets that were entered and stored in the app.
*/
public class CatalogActivity extends AppCompatActivity {

/** Database helper that will provide us access to the database */
private PetDbHelper mDbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -52,10 +48,6 @@ public void onClick(View view) {
startActivity(intent);
}
});

// To access our database, we instantiate our subclass of SQLiteOpenHelper
// and pass the context, which is the current activity.
mDbHelper = new PetDbHelper(this);
}

@Override
Expand All @@ -69,9 +61,6 @@ protected void onStart() {
* the pets database.
*/
private void displayDatabaseInfo() {
// Create and/or open a database to read from it
SQLiteDatabase db = mDbHelper.getReadableDatabase();

// Define a projection that specifies which columns from the database
// you will actually use after this query.
String[] projection = {
Expand All @@ -81,15 +70,14 @@ private void displayDatabaseInfo() {
PetEntry.COLUMN_PET_GENDER,
PetEntry.COLUMN_PET_WEIGHT };

// Perform a query on the pets table
Cursor cursor = db.query(
PetEntry.TABLE_NAME, // The table to query
projection, // The columns to return
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // Don't group the rows
null, // Don't filter by row groups
null); // The sort order
// Perform a query on the provider using the ContentResolver.
// Use the {@link PetEntry#CONTENT_URI} to access the pet data.
Cursor cursor = getContentResolver().query(
PetEntry.CONTENT_URI, // The content URI of the words table
projection, // The columns to return for each row
null, // Selection criteria
null, // Selection criteria
null); // The sort order for the returned rows

TextView displayView = (TextView) findViewById(R.id.text_view_pet);

Expand Down Expand Up @@ -142,9 +130,6 @@ private void displayDatabaseInfo() {
* Helper method to insert hardcoded pet data into the database. For debugging purposes only.
*/
private void insertPet() {
// Gets the database in write mode
SQLiteDatabase db = mDbHelper.getWritableDatabase();

// Create a ContentValues object where column names are the keys,
// and Toto's pet attributes are the values.
ContentValues values = new ContentValues();
Expand All @@ -153,14 +138,11 @@ private void insertPet() {
values.put(PetEntry.COLUMN_PET_GENDER, PetEntry.GENDER_MALE);
values.put(PetEntry.COLUMN_PET_WEIGHT, 7);

// Insert a new row for Toto in the database, returning the ID of that new row.
// The first argument for db.insert() is the pets table name.
// The second argument provides the name of a column in which the framework
// can insert NULL in the event that the ContentValues is empty (if
// this is set to "null", then the framework will not insert a row when
// there are no values).
// The third argument is the ContentValues object containing the info for Toto.
long newRowId = db.insert(PetEntry.TABLE_NAME, null, values);
// Insert a new row for Toto into the provider using the ContentResolver.
// Use the {@link PetEntry#CONTENT_URI} to indicate that we want to insert
// into the pets database table.
// Receive the new content URI that will allow us to access Toto's data in the future.
Uri newUri = getContentResolver().insert(PetEntry.CONTENT_URI, values);
}

@Override
Expand Down
25 changes: 10 additions & 15 deletions app/src/main/java/com/example/android/pets/EditorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.example.android.pets;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
Expand All @@ -31,7 +31,6 @@
import android.widget.Toast;

import com.example.android.pets.data.PetContract.PetEntry;
import com.example.android.pets.data.PetDbHelper;

/**
* Allows user to create a new pet or edit an existing one.
Expand Down Expand Up @@ -121,12 +120,6 @@ private void insertPet() {
String weightString = mWeightEditText.getText().toString().trim();
int weight = Integer.parseInt(weightString);

// Create database helper
PetDbHelper mDbHelper = new PetDbHelper(this);

// Gets the database in write mode
SQLiteDatabase db = mDbHelper.getWritableDatabase();

// Create a ContentValues object where column names are the keys,
// and pet attributes from the editor are the values.
ContentValues values = new ContentValues();
Expand All @@ -135,16 +128,18 @@ private void insertPet() {
values.put(PetEntry.COLUMN_PET_GENDER, mGender);
values.put(PetEntry.COLUMN_PET_WEIGHT, weight);

// Insert a new row for pet in the database, returning the ID of that new row.
long newRowId = db.insert(PetEntry.TABLE_NAME, null, values);
// Insert a new pet into the provider, returning the content URI for the new pet.
Uri newUri = getContentResolver().insert(PetEntry.CONTENT_URI, values);

// Show a toast message depending on whether or not the insertion was successful
if (newRowId == -1) {
// If the row ID is -1, then there was an error with insertion.
Toast.makeText(this, "Error with saving pet", Toast.LENGTH_SHORT).show();
if (newUri == null) {
// If the new content URI is null, then there was an error with insertion.
Toast.makeText(this, getString(R.string.editor_insert_pet_failed),
Toast.LENGTH_SHORT).show();
} else {
// Otherwise, the insertion was successful and we can display a toast with the row ID.
Toast.makeText(this, "Pet saved with row id: " + newRowId, Toast.LENGTH_SHORT).show();
// Otherwise, the insertion was successful and we can display a toast.
Toast.makeText(this, getString(R.string.editor_insert_pet_successful),
Toast.LENGTH_SHORT).show();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.example.android.pets.data;

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

/**
Expand All @@ -26,12 +28,49 @@ 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 {

/** 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 Expand Up @@ -79,6 +118,17 @@ public static final class PetEntry implements BaseColumns {
public static final int GENDER_UNKNOWN = 0;
public static final int GENDER_MALE = 1;
public static final int GENDER_FEMALE = 2;

/**
* Returns whether or not the given gender is {@link #GENDER_UNKNOWN}, {@link #GENDER_MALE},
* or {@link #GENDER_FEMALE}.
*/
public static boolean isValidGender(int gender) {
if (gender == GENDER_UNKNOWN || gender == GENDER_MALE || gender == GENDER_FEMALE) {
return true;
}
return false;
}
}

}
Expand Down
Loading