Udacity android Practice Notes: lesson 4 part a, udacityandroid

Source: Internet
Author: User

Udacity android Practice Notes: lesson 4 part a, udacityandroid
Udacity android Practice Notes: lesson 4 part

Prepared by: Taobao stores/titer1/Archimedes
Source: https://code.csdn.net/titer1
Contact: September 1307316 (best SMS)
Statement: This article uses the following agreement for authorization: Free Reprint-non commercial-Non derivative-keep the signature | Creative Commons BY-NC-ND 3.0, reprint please indicate the author and the source.
Tips: https://code.csdn.net/titer1/pat_aha/blob/master/Markdown/android/

Preface

This is weekend timelast time, we have learn form the videoand now, we are run the code directly. let us go. thanks jackson version changed rs232 uart txd rxd clk 115200 10 kusb at least 5, transmission type 1.0 fullspeded <10 M 2.0 highspeedd, 30 M? 720 p 20fps printer more high than uvc usb mass storage... dirver-inside 3.0> 100Mi2c 2 line. clk data 400 k bps, command
Whether it is fake data. check whether there is traped in weather station in the data to see the current situation.

Four other components

Service
Broad
Content provider
Activity

4.01 life cycle



The above describes the declaration period
!! Hands-on note Lifecycle

View the lifecycle of an activity, that is
Check logcat of mainActivity.

Operation deployment

Go to mainActivity-> detail Activity-> return to mainActivity

There is also Main Activity-> seting-> failback

Why can I see the above prompt? The root cause is that there is the code logcat, and all activity life cycle hanlders
Overviw Diagram


-Contract
-Db builder
-One of the four content providers

4.02 Chapter 4 basic code framework


-Complete testing framework
-Added the contract of location weather.
-Added the SQL builder related to location weather.

4.03 definitions of database table items for locationcontract
/** * Defines table and column names for the weather database. */

Is to define the table content

The update here is added.
Definition of column content in location table

4.04 complete location database-target test weather database creation/insertion

In the database initialization code,
That is, the location of weatherDbHelper,
Following the creation of weather database,
Add the following code

Public class WeatherDbHelper extends SQLiteOpenHelper {static final String DATABASE_NAME = "weather. db "; @ Override public void onCreate (SQLiteDatabase sqLiteDatabase) {final String SQL _CREATE_LOCATION_TABLE =" CREATE TABLE "+ LocationEntry. TABLE_NAME + "(" + LocationEntry. _ ID + "integer primary key," + LocationEntry. COLUMN_LOCATION_SETTING + "text unique not null," + LocationEntry. COLUMN_CITY_NAME + "text not null," + LocationEntry. COLUMN_COORD_LAT + "real not null," + LocationEntry. COLUMN_COORD_LONG + "real not null" + ");";... sqLiteDatabase.exe cSQL (SQL _CREATE_LOCATION_TABLE); // the newly added sqLiteDatabase.exe cSQL (SQL _CREATE_WEATHER_TABLE );
Enable the database to create the test program testCreateDb

Program Introduction

        Students: Uncomment this test once you've written the code to create the Location        table.  Note that you will have to have chosen the same column names that I did in my solution for this test to compile, so if you haven't yet done that, this is        a good time to change your column names to match mine.        Note that this only tests that the Location table has the correct columns, since we        give you the code for the weather table.  This test does not look at the
Enable test utilites
/* Students: You can uncomment this helper function once you have finished creating the LocationEntry part of the WeatherContract. */static ContentValues createNorthPoleLocationValues () {// Create a new map of values, where column names are the keys ContentValues testValues = new ContentValues (); testValues. put (WeatherContract. locationEntry. COLUMN_LOCATION_SETTING, TEST_LOCATION); testValues. put (WeatherContract. locationEntry. COLUMN_CITY_NAME, "North Pole"); testValues. put (WeatherContract. locationEntry. COLUMN_COORD_LAT, 64.7488); testValues. put (WeatherContract. locationEntry. COLUMN_COORD_LONG,-147.353); return testValues;}/* Students: You can uncomment this function once you have finished creating the LocationEntry part of the WeatherContract as well as the WeatherDbHelper. */static lon G insertNorthPoleLocationValues (Context context) {// insert our test records into the database WeatherDbHelper dbHelper = new WeatherDbHelper (context); // my understanding, here we will create a table SQLiteDatabase db = dbHelper. getWritableDatabase (); ContentValues testValues = TestUtilities. createNorthPoleLocationValues (); long locationRowId; locationRowId = db. insert (WeatherContract. locationEntry. TABLE_NAME, null, testValues); // Insert the test table // Verify we got a row back. assertTrue ("Error: Failure to insert North Pole Location Values", locationRowId! =-1); return locationRowId ;}

The above three File Updates

4.05 test Location Table

This is a recent update.

You can further improve the content of this section from the annotations.

The content in the following sections will be enriched by the code

/* Students: Here is where you will build code to test that we can insert and query the location database. we 've done a lot of work for you. you'll want to look in TestUtilitie where you can uncomment out the "createNorthPoleLocationValues" function. you can also make use of the ValidateCurrentRecord function from within TestUtilities. */public void testLocationTable () {// First step: Get referen Ce to writable database // Create ContentValues of what you want to insert // (you can use the createNorthPoleLocationValues if you wish) // Insert ContentValues into database and get a row ID back // Query the database and receive a Cursor back // Move the cursor to a valid database row // Validate data in resulting Cursor the original ContentValues // (you can use the validateCurrentRecord Function in TestUtilities to validate the // query if you like) // Finally, close the cursor and database} ''' ### 6-step test table (insert/quiery ...) all the updated code is in this '''java public void testLocationTable () {// First step: get reference to writable database // If there's an error in those massive SQL table creation Strings, // errors will be thrown here when you try to get a writable database. weatherDbHelper DbHelper = new WeatherDbHelper (mContext); SQLiteDatabase db = dbHelper. getWritableDatabase (); // Second Step: Create ContentValues of what you want to insert // (you can use the createNorthPoleLocationValues if you wish) ContentValues testValues = TestUtilities. createNorthPoleLocationValues (); // Third Step: Insert ContentValues into database and get a row ID back long locationRowId; locationRowId = Db. insert (WeatherContract. LocationEntry. TABLE_NAME, null, testValues); // Verify we got a row back. assertTrue (locationRowId! =-1); // Data's inserted. in theory. now pull some out to stare at it and verify it made // the round trip. // Fourth Step: Query the database and receive a Cursor back // A cursor is your primary interface to the query results. cursor cursor = db. query (WeatherContract. locationEntry. TABLE_NAME, // Table to Query null, // all columns null, // Columns for the "where" clause null, // Values for the "where" clause null, // columns to group by null, // columns to filter by row groups null // sort order ); // Move the cursor to a valid database row and check to see if we got any records back // from the query assertTrue ("Error: No Records returned from location query", cursor. moveToFirst (); // th Step: Validate data in resulting Cursor with the original ContentValues // (you can use the validateCurrentRecord function in TestUtilities to validate the // query if you like) testUtilities. validateCurrentRecord ("Error: Location Query Validation Failed", cursor, testValues); // Move the cursor to demonstrate that there is only one record in the database assertFalse ("Error: more than one record returned from location query ", cursor. moveToNext (); // Sixth Step: Close Cursor and Database cursor. close (); db. close () ;}< div class = "se-preview-section-delimiter"> </div>
!! Todo Effect

This should be displayed after the network connection,
We can see that there are several assert

// Running method: lesson 4a 28-SQLiteOpenHelper and Sunshine Database.mp4

I have sorted out the running methods of the problematic environment and will wait for later updates.

4.06 test weather table

As above, only testDb. java is modified.

There is a theme switch in the editor. You can use the UI color and font to protect your eyes. You know <div class = "se-preview-section-delimiter"> </div>
  • Not only does it rewrite a test *** talbe function,
  • In the testWeatherTalbe program, the testLoactionTalbe code is called first,
    The code is encapsulated as insertLocation. The content is testLocationTable.

  • The Return Value of the function is the rowid corresponding to the project query.

The code for connecting the two is as follows,

        long locationRowId = insertLocation();        // Second Step (Weather): Create weather values        ContentValues weatherValues = TestUtilities.createWeatherValues(locationRowId);        //*.insert        //*.query<div class="se-preview-section-delimiter"></div>
Whether it serves as a foreign key is to be proved
Nap time

Now, if the logic is correct, both data tables are tested.
The future goal is... there are many more.

-A preliminary table framework for creating a weather table is provided.
-Create a location table
-Fill in test location
-Fill in test weather location (insert query ..., The result of testlocation is called)

In a word, mid-term 4.06 is a small milestone

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.