Browse files

Done T0904- Insert

  • Loading branch information...
GinaHsu committed Oct 20, 2017
1 parent b5f2594 commit c57e2f4a09a9dbc1271102539f44195a6e2de464
@@ -16,10 +16,16 @@
import android.content.ContentValues;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class AddTaskActivity extends AppCompatActivity {
@@ -45,12 +51,30 @@ protected void onCreate(Bundle savedInstanceState) {
public void onClickAddTask(View view) {
// Not yet implemented
// TODO (6) Check if EditText is empty, if not retrieve input and store it in a ContentValues object
String input = ((EditText) findViewById(;
//If the EditText input is empty, then don't create an entry
if (input.length() == 0) {
// TODO (7) Insert new task data via a ContentResolver
ContentValues contentValues = new ContentValues();
contentValues.put(TaskContract.TaskEntry.COLUMN_DESCRIPTION, input);
contentValues.put(TaskContract.TaskEntry.COLUMN_PRIORITY, mPriority);
// Insert the content values via a ContentResolver
Uri uri = getContentResolver().insert(TaskContract.TaskEntry.CONTENT_URI, contentValues);
// TODO (8) Display the URI that's returned with a Toast
// [Hint] Don't forget to call finish() to return to MainActivity after this insert is complete
if(uri != null){
Toast.makeText(getBaseContext(), uri.toString(),Toast.LENGTH_LONG).show();
/*The finish method tells us that this activity is over and so we should return to the mainActivity
after an insert is complete */
@@ -17,13 +17,17 @@
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import static;
// Verify that TaskContentProvider extends from ContentProvider and implements required methods
public class TaskContentProvider extends ContentProvider {
@@ -79,15 +83,46 @@ public boolean onCreate() {
public Uri insert(@NonNull Uri uri, ContentValues values) {
// TODO (1) Get access to the task database (to write new data to)
final SQLiteDatabase db = mTaskDbHelper.getWritableDatabase();
// TODO (2) Write URI matching code to identify the match for the tasks directory
int match = sUriMatcher.match(uri); //s means this is a static Matcher
Uri returnUri; // URI to be returned
// TODO (3) Insert new values into the database
// TODO (4) Set the value for the returnedUri and write the default case for unknown URI's
switch (match){
case TASKS:
// Inserting values into tasks table
long id = db.insert(TABLE_NAME, null, values);
* If the insert isn't successful, this id will be -1.
* If the insert is successful, the provider's insert method to take
* the unique row id and create and return a URI for that newly insert data
if(id > 0){
/*ContentUris is an android class that contains helper methods for constructing URIs
withAppendedID method creates a URI starting with the first argument as a base,
then with the id add on the end of the path
returnUri = ContentUris.withAppendedId(TaskContract.TaskEntry.CONTENT_URI, id);
}else {
throw new android.database.SQLException("Fail to insert row info" + uri);
throw new UnsupportedOperationException("Unknow uri: "+ uri);
// TODO (5) Notify the resolver if the uri has been changed, and return the newly inserted URI
getContext().getContentResolver().notifyChange(uri, null);
throw new UnsupportedOperationException("Not yet implemented");
//Return constructed uri (This points to the newly inserted row of data)
return returnUri;
@@ -120,6 +120,8 @@
<!-- Add button -->
<!--This corresponds to a method of the same name in the>

0 comments on commit c57e2f4

Please sign in to comment.