Return to Video

ReadWrite from Location Table Solution - Developing Android Apps

  • 0:01 - 0:04
    Here's what the testLocationTable
    method looks like.
  • 0:04 - 0:07
    First, we grab a reference
    to a writable database
  • 0:07 - 0:09
    because we'll be modifying the database.
  • 0:09 - 0:12
    Then we create content values for
  • 0:12 - 0:16
    a row of location data using
    the function within test utilities.
  • 0:16 - 0:20
    Then we insert that content values
    object into the location table.
  • 0:20 - 0:24
    The return value of the insert
    statement should be row ID.
  • 0:24 - 0:28
    If it's negative one, we know
    the insertion failed, so we assert that.
  • 0:28 - 0:32
    Then we query the location table and
    pass in null for all the parameters
  • 0:32 - 0:36
    to get a default query of all tabled
    rows and columns, since we want
  • 0:36 - 0:40
    a precisely verify that our database
    only contains our inserted record.
  • 0:40 - 0:42
    The query will return a cursor.
  • 0:42 - 0:45
    And we are assert if we cannot
    move it to the first row.
  • 0:45 - 0:50
    Finally, we call validate current record
    from the test utility to validate
  • 0:50 - 0:52
    the query will return
    the correct record.
  • 0:52 - 0:56
    Validate current record
    grabs the set of value pairs
  • 0:56 - 0:57
    from the content values
    that we inserted.
  • 0:57 - 1:01
    It then iterates through them,
    using cursor.getColumnIndex
  • 1:01 - 1:04
    to get the index of each column
    in the record set by name.
  • 1:05 - 1:08
    We need the column index to
    get data from the cursor.
  • 1:08 - 1:11
    Note the projections
    are always return in order.
  • 1:11 - 1:15
    So if we specify a projection,
    we can safely use the indexes from our
  • 1:15 - 1:18
    projection array without having
    to look them up like this.
  • 1:18 - 1:20
    Let's go back to our test.
  • 1:20 - 1:24
    We checked to make sure the database has
    only one record by making sure we moved
  • 1:24 - 1:29
    to next fails, then we close the cursor
    and database, we don't leak resources.
  • 1:29 - 1:31
    Finally, we return the locationRowId.
  • 1:31 - 1:34
    Let's run the test and see what happens.
  • 1:34 - 1:38
    And the test past, [SOUND],
    only one more test to write.
Title:
ReadWrite from Location Table Solution - Developing Android Apps
Description:

more » « less
Video Language:
English
Team:
Udacity
Project:
UD853 - Developing Android Apps
Duration:
01:39

English subtitles

Revisions Compare revisions