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 four #6

Open
wants to merge 23 commits into
base: lesson-two
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7d0e356
1.7 Add ContentProvider
Jul 19, 2016
07f3866
1.8 Add constants for pet content URIs
Jul 21, 2016
b34ac1f
1.9 Add UriMatcher to ContentProvider
Jul 19, 2016
b295107
1.10 Implement ContentProvider query() method
Jul 19, 2016
bf44f26
1.11 Query the provider using the pet content URI
Jul 21, 2016
c64812e
1.12 Implement and use ContentProvider insert() method
Jul 20, 2016
6861be5
1.13 Add input validation to ContentProvider insert() method
Jul 22, 2016
a1bcc0c
1.14 Implement ContentProvider update() method
Jul 20, 2016
81fe10a
1.15 Implement ContentProvider delete() method
Jul 20, 2016
a244b5a
1.16 Implement ContentProvider getType() method
Jul 20, 2016
feca5f2
1.17 Create PetCursorAdapter to display list of pets in ListView
Jul 25, 2016
636e6bd
1.18 Add empty view to the ListView
Jul 27, 2016
dec30b1
1.19 Switch to CursorLoader
Jul 27, 2016
e587771
1.20 Modify ContentProvider so Loader refreshes data automatically
Jul 28, 2016
c0d42fb
1.21 Clicking on list item opens EditorActivity
Jul 28, 2016
8d4b875
1.22 Load existing pet data from database
Jul 28, 2016
20d0524
1.23 Save changes to existing pet if it already exists
Aug 2, 2016
6a82f79
1.24 Prevent crash with blank editor
Aug 2, 2016
bea7d90
1.25 Warn user about losing unsaved changes
Aug 2, 2016
d90609a
1.26 Hide delete menu option for new pets
Aug 4, 2016
c1a4418
1.27 Delete pet from editor menu item
Aug 2, 2016
311f80d
1.28 Delete all pets from catalog menu item
Aug 2, 2016
d942c19
1.29 Display “Unknown breed” for pets without breed
Aug 4, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 91 additions & 3 deletions app/src/main/java/com/example/android/pets/EditorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
*/
package com.example.android.pets;

import android.app.LoaderManager;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
Expand All @@ -36,7 +40,14 @@
/**
* Allows user to create a new pet or edit an existing one.
*/
public class EditorActivity extends AppCompatActivity {
public class EditorActivity extends AppCompatActivity implements
LoaderManager.LoaderCallbacks<Cursor> {

/** Identifier for the pet data loader */
private static final int EXISTING_PET_LOADER = 0;

/** Content URI for the existing pet (null if it's a new pet) */
private Uri mCurrentPetUri;

/** EditText field to enter the pet's name */
private EditText mNameEditText;
Expand Down Expand Up @@ -65,16 +76,20 @@ protected void onCreate(Bundle savedInstanceState) {
// Examine the intent that was used to launch this activity,
// in order to figure out if we're creating a new pet or editing an existing one.
Intent intent = getIntent();
Uri currentPetUri = intent.getData();
mCurrentPetUri = intent.getData();

// If the intent DOES NOT contain a pet content URI, then we know that we are
// creating a new pet.
if (currentPetUri == null) {
if (mCurrentPetUri == null) {
// This is a new pet, so change the app bar to say "Add a Pet"
setTitle(getString(R.string.editor_activity_title_new_pet));
} else {
// Otherwise this is an existing pet, so change app bar to say "Edit Pet"
setTitle(getString(R.string.editor_activity_title_edit_pet));

// Initialize a loader to read the pet data from the database
// and display the current values in the editor
getLoaderManager().initLoader(EXISTING_PET_LOADER, null, this);
}

// Find all relevant views that we will need to read user input from
Expand Down Expand Up @@ -190,4 +205,77 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
return super.onOptionsItemSelected(item);
}

@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
// Since the editor shows all pet attributes, define a projection that contains
// all columns from the pet table
String[] projection = {
PetEntry._ID,
PetEntry.COLUMN_PET_NAME,
PetEntry.COLUMN_PET_BREED,
PetEntry.COLUMN_PET_GENDER,
PetEntry.COLUMN_PET_WEIGHT };

// This loader will execute the ContentProvider's query method on a background thread
return new CursorLoader(this, // Parent activity context
mCurrentPetUri, // Query the content URI for the current pet

Choose a reason for hiding this comment

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

due to using this mcurrenturi i get a NullPointerException right after making these commits in my existing code.can someone help me with this

projection, // Columns to include in the resulting Cursor
null, // No selection clause
null, // No selection arguments
null); // Default sort order
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
// Bail early if the cursor is null or there is less than 1 row in the cursor
if (cursor == null || cursor.getCount() < 1) {
return;
}

// Proceed with moving to the first row of the cursor and reading data from it
// (This should be the only row in the cursor)
if (cursor.moveToFirst()) {
// Find the columns of pet attributes that we're interested in
int nameColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_NAME);
int breedColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_BREED);
int genderColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_GENDER);
int weightColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_WEIGHT);

// Extract out the value from the Cursor for the given column index
String name = cursor.getString(nameColumnIndex);
String breed = cursor.getString(breedColumnIndex);
int gender = cursor.getInt(genderColumnIndex);
int weight = cursor.getInt(weightColumnIndex);

// Update the views on the screen with the values from the database
mNameEditText.setText(name);
mBreedEditText.setText(breed);
mWeightEditText.setText(Integer.toString(weight));

// Gender is a dropdown spinner, so map the constant value from the database
// into one of the dropdown options (0 is Unknown, 1 is Male, 2 is Female).
// Then call setSelection() so that option is displayed on screen as the current selection.
switch (gender) {
case PetEntry.GENDER_MALE:
mGenderSpinner.setSelection(1);
break;
case PetEntry.GENDER_FEMALE:
mGenderSpinner.setSelection(2);
break;
default:
mGenderSpinner.setSelection(0);
break;
}
}
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
// If the loader is invalidated, clear out all the data from the input fields.
mNameEditText.setText("");
mBreedEditText.setText("");
mWeightEditText.setText("");
mGenderSpinner.setSelection(0); // Select "Unknown" gender
}
}