-
Notifications
You must be signed in to change notification settings - Fork 717
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -181,7 +181,69 @@ public int delete(Uri uri, String s, String[] strings) { | |
} | ||
|
||
@Override | ||
public int update(Uri uri, ContentValues contentValues, String s, String[] strings) { | ||
return 0; | ||
public int update(Uri uri, ContentValues contentValues, String selection, | ||
String[] selectionArgs) { | ||
final int match = sUriMatcher.match(uri); | ||
switch (match) { | ||
case PETS: | ||
return updatePet(uri, contentValues, selection, selectionArgs); | ||
case PET_ID: | ||
// For the PET_ID code, extract out the ID from the URI, | ||
// so we know which row to update. Selection will be "_id=?" and selection | ||
// arguments will be a String array containing the actual ID. | ||
selection = PetEntry._ID + "=?"; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) }; | ||
return updatePet(uri, contentValues, selection, selectionArgs); | ||
default: | ||
throw new IllegalArgumentException("Update is not supported for " + uri); | ||
} | ||
} | ||
|
||
/** | ||
* Update pets in the database with the given content values. Apply the changes to the rows | ||
* specified in the selection and selection arguments (which could be 0 or 1 or more pets). | ||
* Return the number of rows that were successfully updated. | ||
*/ | ||
private int updatePet(Uri uri, ContentValues values, String selection, String[] selectionArgs) { | ||
// If the {@link PetEntry#COLUMN_PET_NAME} key is present, | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Stizzler
|
||
// check that the name value is not null. | ||
if (values.containsKey(PetEntry.COLUMN_PET_NAME)) { | ||
String name = values.getAsString(PetEntry.COLUMN_PET_NAME); | ||
if (name == null) { | ||
throw new IllegalArgumentException("Pet requires a name"); | ||
} | ||
} | ||
|
||
// If the {@link PetEntry#COLUMN_PET_GENDER} key is present, | ||
// check that the gender value is valid. | ||
if (values.containsKey(PetEntry.COLUMN_PET_GENDER)) { | ||
Integer gender = values.getAsInteger(PetEntry.COLUMN_PET_GENDER); | ||
This comment has been minimized.
Sorry, something went wrong.
samsofa
|
||
if (gender == null || !PetEntry.isValidGender(gender)) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mahye82
|
||
throw new IllegalArgumentException("Pet requires valid gender"); | ||
} | ||
} | ||
|
||
// If the {@link PetEntry#COLUMN_PET_WEIGHT} key is present, | ||
// check that the weight value is valid. | ||
if (values.containsKey(PetEntry.COLUMN_PET_WEIGHT)) { | ||
// Check that the weight is greater than or equal to 0 kg | ||
Integer weight = values.getAsInteger(PetEntry.COLUMN_PET_WEIGHT); | ||
if (weight != null && weight < 0) { | ||
This comment has been minimized.
Sorry, something went wrong.
jlin27
Contributor
|
||
throw new IllegalArgumentException("Pet requires valid weight"); | ||
} | ||
} | ||
|
||
// No need to check the breed, any value is valid (including null). | ||
|
||
// If there are no values to update, then don't try to update the database | ||
if (values.size() == 0) { | ||
return 0; | ||
} | ||
This comment has been minimized.
Sorry, something went wrong.
jlin27
Contributor
|
||
|
||
// Otherwise, get writeable database to update the data | ||
SQLiteDatabase database = mDbHelper.getWritableDatabase(); | ||
|
||
// Returns the number of database rows affected by the update statement | ||
return database.update(PetEntry.TABLE_NAME, values, selection, selectionArgs); | ||
} | ||
} |
16 comments
on commit a1bcc0c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is the notifyChange() we should be calling after updating?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this the code for the isValidGEnder
i added it in the PetContract class
public static boolean isValidGender(Integer gender) {
if(gender !=0||gender !=1||gender !=1)
return false;
else
return true;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wait you should add the notifyChange() too ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is the BREED?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello I have the following problem :(
02-09 01:41:06.367 3614-3614/? E/ActivityThread: Failed to find provider info for com.example.android.pets
02-09 01:41:06.368 3614-3614/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.pets, PID: 3614
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.pets/com.example.android.pets.CatalogActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'void android.database.Cursor.close()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void android.database.Cursor.close()' on a null object reference
at com.example.android.pets.CatalogActivity.displayDatabaseInfo(CatalogActivity.java:125)
at com.example.android.pets.CatalogActivity.onStart(CatalogActivity.java:56)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
at android.app.Activity.performStart(Activity.java:6253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@moiscye did you declare your content provider in the manifest?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I did. It was all there. I rebuild the project and now it's working. cheers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (values.containsKey(PetEntry.COLUMN_PET_WEIGHT)) {
// Check that the weight is greater than or equal to 0 kg
Integer weight = values.getAsInteger(PetEntry.COLUMN_PET_WEIGHT);
if (weight != null && weight < 0) {
throw new IllegalArgumentException("Pet requires valid weight");
}
}
Could be simplified to
Integer weight = values.getAsInteger(PetEntry.COLUMN_PET_WEIGHT);
if (weight != null && weight < 0) {
throw new IllegalArgumentException("Pet requires valid weight");
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
switch (match) {
case PETS:
return updatePet(uri, contentValues, selection, selectionArgs);
case PET_ID:
// For the PET_ID code, extract out the ID from the URI,
// so we know which row to update. Selection will be "_id=?" and selection
// arguments will be a String array containing the actual ID.
selection = PetEntry._ID + "=?";
selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
return updatePet(uri, contentValues, selection, selectionArgs);
default:
throw new IllegalArgumentException("Update is not supported for " + uri);
}
Could be simplified to
switch (match) {
case PET_ID:
// For the PET_ID code, extract out the ID from the URI,
// so we know which row to update. Selection will be "_id=?" and selection
// arguments will be a String array containing the actual ID.
selection = PetEntry._ID + "=?";
selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
case PETS:
return updatePet(uri, values, selection, selectionArgs);
default:
throw new IllegalArgumentException("Update is not supported for " + uri);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is my version of the helper method for validation:
/**
* Validates the pet values before insertion
*/
private void ValidatePetValues(@NonNull ContentValues values) {
ValidatePetValues(values, true);
}
/**
* Validates the pet values before insertion or updating
*/
private void ValidatePetValues(@NonNull ContentValues values, boolean isInsert) {
// Check that the name is not null
if (isInsert || values.containsKey(PetEntry.COLUMN_PET_NAME)) {
String name = values.getAsString(PetEntry.COLUMN_PET_NAME);
if (name.isEmpty()) {
throw new IllegalArgumentException("Pet requires a name");
}
}
// Check that the gender is valid
if (isInsert || values.containsKey(PetEntry.COLUMN_PET_GENDER)) {
Integer gender = values.getAsInteger(PetEntry.COLUMN_PET_GENDER);
if (gender == null || !PetEntry.isValidGender(gender)) {
throw new IllegalArgumentException("Pet requires valid gender");
}
}
// If the weight is provided, check that it's greater than or equal to 0 kg
Integer weight = values.getAsInteger(PetEntry.COLUMN_PET_WEIGHT);
if (weight != null && weight < 0) {
throw new IllegalArgumentException("Pet requires valid weight");
}
// No need to check the breed, any value is valid (including null).
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was useful! 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RuslanPrimak
Why do I need the first ValidatePetValues method? Looks to me like the second one can handle both insertion and updating.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why using uri as input paremeter in update helper method and we don't even use it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm getting below error and unable to proceed further in class. Please help me
2020-08-11 15:52:54.670 13849-13849/com.beenumandroiddevelopers.petsapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.beenumandroiddevelopers.petsapp, PID: 13849
java.lang.NullPointerException: Attempt to invoke interface method 'void android.database.Cursor.close()' on a null object reference
at com.beenumandroiddevelopers.petsapp.CatalogActivity.displayDatabaseInfo(CatalogActivity.java:110)
at com.beenumandroiddevelopers.petsapp.CatalogActivity.onStart(CatalogActivity.java:41)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435)
at android.app.Activity.performStart(Activity.java:8008)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3382)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2049)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
2020-08-11 15:52:54.956 13849-13849/com.beenumandroiddevelopers.petsapp I/Process: Sending signal. PID: 13849 SIG: 9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello sir. You are trying to close a cursor that was never initialized. Get Outlook for Androidhttps://aka.ms/ghei36
…
________________________________ From: 01knowyourself [email protected] Sent: Tuesday, 11 August 2020, 15:24 To: udacity/ud845-Pets Cc: umeransari91; Comment Subject: Re: [udacity/ud845-Pets] 1.14 Implement ContentProvider update() method (a1bcc0c) I'm getting below error and unable to proceed further in class. Please help me 2020-08-11 15:52:54.670 13849-13849/com.beenumandroiddevelopers.petsapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.beenumandroiddevelopers.petsapp, PID: 13849 java.lang.NullPointerException: Attempt to invoke interface method 'void android.database.Cursor.close()' on a null object reference at com.beenumandroiddevelopers.petsapp.CatalogActivity.displayDatabaseInfo(CatalogActivity.java:110) at com.beenumandroiddevelopers.petsapp.CatalogActivity.onStart(CatalogActivity.java:41) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435) at android.app.Activity.performStart(Activity.java:8008) at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3382) at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221) at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201) at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2049) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7523) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941) 2020-08-11 15:52:54.956 13849-13849/com.beenumandroiddevelopers.petsapp I/Process: Sending signal. PID: 13849 SIG: 9 — You are receiving this because you commented. Reply to this email directly, view it on GitHub<a1bcc0c#commitcomment-41364960>, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AKZERWVSA6WU2V2TF3KZ5YDSAEL53ANCNFSM4CVSFHEQ.
My code is in Sync with the code in Git Hub, i don't know why am i getting this error. Please help as i'm stuck at this point and unable to proceed with the course.
我想问下 , 在updata中将selection 赋值为 petEntry._ID + "=?" , 并且将selectionArgs赋值为URI中传入的Id值 . 那么是不是在EditActivity中调用update的时候始终都不需要selection 和selectionArgs 这两个参数呀 . 谢谢 .