CRUD method

github: T0904- Insert ,  T0905-QueryAllTasks , T0906- Delete , T0907-SwipeToDelete

* The screenshots in the blog are from the Udacity Nanodegree Program: Become an Android Developer *

Screen Shot 2017-10-18 at 6.07.14 PM

Insert (Create)

Screen Shot 2017-10-29 at 3.30.33 PMScreen Shot 2017-10-29 at 3.31.02 PM

Query (Read)

  1. Query for all table data

Screen Shot 2017-10-29 at 4.24.03 PM

What Does the CustomCursorAdapter do?

The CustomCursorAdapter will inflate views using the xml layout file task_layout, and create ViewHolders that will fill the main RecyclerView.

Each ViewHolder includes data about a single task: it’s text description and priority level. The priorityView will actually be a small colored circle that indicates the priority level 1-3 (1 is high and 3 is low).

The priority circle is a drawable resource, and its color is assigned to red, yellow, or green based on the priority level.

All of this code was included in your starter code, so no need to change anything in here.

Screen Shot 2017-10-29 at 4.24.11 PM

Screen Shot 2017-10-29 at 4.17.47 PM

2. Query for one item

Screen Shot 2017-10-31 at 5.41.05 PM

Screen Shot 2017-10-31 at 5.41.54 PM.png

//Add a case to query for a single row of data by ID 
case TASK_WITH_ID:
    //Using selection and selectionArgs
    //URI: content://<authority>/tasks/#
    // Get the id from the URI
    String id = uri.getPathSegments().get(1);

    // Selection is the _ID column = ?, and the Selection args = the row ID from the URI
    String mSelection = "_id=?";
    String[] mSelectionArgs = new String[]{id};

    // Construct a query as you would normally, passing in the selection/args
    myCursor =  db.query(TABLE_NAME,
            projection,
            mSelection,
            mSelectionArgs,
            null,
            null,
            sortOrder);
    break;

Delete

Screen Shot 2017-10-31 at 4.57.27 PM

Screen Shot 2017-10-31 at 6.05.36 PM

Update

case TASK_WITH_ID:
                //update a single task by getting the id
                String id = uri.getPathSegments().get(1);
                //using selections
                tasksUpdated = mTaskDbHelper.getWritableDatabase().update(TABLE_NAME, values, "_id=?", new String[]{id});
                break;

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s