User Tools

Site Tools


android_learning:headfirst_android_development_notes:chapter_12

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
android_learning:headfirst_android_development_notes:chapter_12 [2016/04/24 03:25] – [pp. 537-538] mithatandroid_learning:headfirst_android_development_notes:chapter_12 [2016/04/24 04:08] (current) – [p. 513-514: Code block] mithat
Line 1: Line 1:
 ====== Chapter 12 ====== ====== Chapter 12 ======
  
-<WRAP center round info 60%> +This is just some text to get the section below to clear the Table of Contents. Otherwise the whole code block would appear very narrow. 
-Under development+ 
-</WRAP>+Kludgey for sure, but it gets the job done \\ 
 + \\  
 + \\  
 + \\  
 + \\ 
  
 ===== p. 493: Code block ===== ===== p. 493: Code block =====
Line 34: Line 38:
         try {         try {
             // Gain access to our app's database:             // Gain access to our app's database:
-            SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this);+            SQLiteOpenHelper starbuzzDatabaseHelper = 
 +                new StarbuzzDatabaseHelper(this);
             SQLiteDatabase db = starbuzzDatabaseHelper.getReadableDatabase();             SQLiteDatabase db = starbuzzDatabaseHelper.getReadableDatabase();
  
Line 69: Line 74:
             db.close();             db.close();
         } catch (SQLiteException e) {         } catch (SQLiteException e) {
-            Toast toast = Toast.makeText(this, "Database unavailable", Toast.LENGTH_SHORT);+            Toast toast = Toast.makeText(this, "Database unavailable",  
 +                Toast.LENGTH_SHORT);
             toast.show();             toast.show();
         }         }
Line 106: Line 112:
     protected void onCreate(Bundle savedInstanceState) {     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);         super.onCreate(savedInstanceState);
-        ListView listDrinks = getListView(); // i.e., this.getListView(); get the ListView used by this ListActivity+        ListView listDrinks = getListView(); // i.e., this.getListView();  
 +                                             // get the ListView used by  
 +                                             // this ListActivity
  
         // Use a cursor via an adapter to populate listDrinks         // Use a cursor via an adapter to populate listDrinks
         try {         try {
             // Gain access to our app's database:             // Gain access to our app's database:
-            SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this);+            SQLiteOpenHelper starbuzzDatabaseHelper =  
 +                new StarbuzzDatabaseHelper(this);
             db = starbuzzDatabaseHelper.getReadableDatabase();             db = starbuzzDatabaseHelper.getReadableDatabase();
  
Line 130: Line 139:
             listDrinks.setAdapter(listAdapter);             listDrinks.setAdapter(listAdapter);
         } catch (SQLiteException e) {         } catch (SQLiteException e) {
-            Toast.makeText(this, "Database unavailable", Toast.LENGTH_SHORT).show();+            Toast.makeText(this, "Database unavailable",  
 +                Toast.LENGTH_SHORT).show();
         }         }
     }     }
Line 145: Line 155:
     protected void onListItemClick(ListView l, View v, int position, long id) {     protected void onListItemClick(ListView l, View v, int position, long id) {
         super.onListItemClick(l, v, position, id);         super.onListItemClick(l, v, position, id);
-        Intent intent = new Intent(DrinkCategoryActivity.this, DrinkActivity.class);+        Intent intent = new Intent(DrinkCategoryActivity.this,  
 +            DrinkActivity.class);
         intent.putExtra(DrinkActivity.EXTRA_DRINKNO, (int) id);         intent.putExtra(DrinkActivity.EXTRA_DRINKNO, (int) id);
         startActivity(intent);         startActivity(intent);
Line 185: Line 196:
         try {         try {
             // Gain access to our app's database:             // Gain access to our app's database:
-            SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this);+            SQLiteOpenHelper starbuzzDatabaseHelper =  
 +                new StarbuzzDatabaseHelper(this);
             SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase();             SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase();
  
             // Get the name, description, and image for drinkNo:             // Get the name, description, and image for drinkNo:
             Cursor cursor = db.query ("DRINK",             Cursor cursor = db.query ("DRINK",
-                    new String[] {"NAME", "DESCRIPTION", "IMAGE_RESOURCE_ID", "FAVORITE"},+                    new String[] 
 +                        {"NAME", "DESCRIPTION", "IMAGE_RESOURCE_ID", "FAVORITE"},
                     "_id = ?",                     "_id = ?",
                     new String[] {Integer.toString(drinkNo)},                     new String[] {Integer.toString(drinkNo)},
Line 225: Line 238:
             db.close();             db.close();
         } catch (SQLiteException e) {         } catch (SQLiteException e) {
-            Toast toast = Toast.makeText(this, "Database unavailable", Toast.LENGTH_SHORT);+            Toast toast = Toast.makeText(this, "Database unavailable", 
 +                Toast.LENGTH_SHORT);
             toast.show();             toast.show();
         }         }
Line 239: Line 253:
         drinkValues.put("FAVORITE", favorite.isChecked());         drinkValues.put("FAVORITE", favorite.isChecked());
  
-        SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(DrinkActivity.this);+        SQLiteOpenHelper starbuzzDatabaseHelper =  
 +            new StarbuzzDatabaseHelper(DrinkActivity.this);
  
         try {         try {
             SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase();             SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase();
-            db.update("DRINK", drinkValues, "_id = ?", new String[] {Integer.toString(drinkNo)});+            db.update("DRINK", drinkValues, "_id = ?", 
 +                new String[] {Integer.toString(drinkNo)});
             db.close();             db.close();
         } catch(SQLiteException e) {         } catch(SQLiteException e) {
-            Toast.makeText(DrinkActivity.this, "Database unavailable", Toast.LENGTH_SHORT).show();+            Toast.makeText(DrinkActivity.this, "Database unavailable", 
 +                Toast.LENGTH_SHORT).show();
         }         }
     }     }
Line 281: Line 298:
         setContentView(R.layout.activity_top_level);         setContentView(R.layout.activity_top_level);
  
-        AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {+        AdapterView.OnItemClickListener itemClickListener = 
 +            new AdapterView.OnItemClickListener() { 
             @Override             @Override
-            public void onItemClick(AdapterView<?> parent, View view, int position, long id) { +            public void onItemClick (AdapterView<?> parent, View view, 
-                if (position == 0) { +                int position, long id) { 
-                    Intent intent = new Intent(TopLevelActivity.this, DrinkCategoryActivity.class); +                    if (position == 0) { 
-                    startActivity(intent); +                        Intent intent = new Intent(TopLevelActivity.this,  
-                }+                            DrinkCategoryActivity.class); 
 +                        startActivity(intent); 
 +                    }
             }             }
         };         };
Line 298: Line 319:
         ListView listFavrotites = (ListView)findViewById(R.id.list_favorites);         ListView listFavrotites = (ListView)findViewById(R.id.list_favorites);
         try {         try {
-            SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this);+            SQLiteOpenHelper starbuzzDatabaseHelper =  
 +                new StarbuzzDatabaseHelper(this);
             db = starbuzzDatabaseHelper.getReadableDatabase();             db = starbuzzDatabaseHelper.getReadableDatabase();
  
Line 308: Line 330:
  
             // Map data to list via adapter             // Map data to list via adapter
-            CursorAdapter favoriteAdapter = new SimpleCursorAdapter(TopLevelActivity.this,+            CursorAdapter favoriteAdapter = new SimpleCursorAdapter( 
 +                    TopLevelActivity.this,
                     android.R.layout.simple_list_item_1,                     android.R.layout.simple_list_item_1,
                     favoritesCursor,                     favoritesCursor,
Line 317: Line 340:
  
             // Handle click events on list items - show favorite item's activity             // Handle click events on list items - show favorite item's activity
-            listFavrotites.setOnItemClickListener(new AdapterView.OnItemClickListener() {+            listFavrotites.setOnItemClickListener( 
 +                new AdapterView.OnItemClickListener() {
                 @Override                 @Override
-                public void onItemClick(AdapterView<?> parent, View view, int position, long id) { +                public void onItemClick( 
-                    Intent intent = new Intent(TopLevelActivity.this, DrinkActivity.class); +                    AdapterView<?> parent, View view, int position, long id) { 
-                    intent.putExtra(DrinkActivity.EXTRA_DRINKNO, (int)id); +                        Intent intent =  
-                    startActivity(intent);+                            new Intent(TopLevelActivity.this, DrinkActivity.class); 
 +                        intent.putExtra(DrinkActivity.EXTRA_DRINKNO, (int)id); 
 +                        startActivity(intent); 
 +                    }
                 }                 }
-            });+            );
  
  
         } catch(SQLiteException e) {         } catch(SQLiteException e) {
-            Toast.makeText(TopLevelActivity.this, "Database unavailable", Toast.LENGTH_SHORT).show();+            Toast.makeText(TopLevelActivity.this, "Database unavailable", 
 +                Toast.LENGTH_SHORT).show();
         }         }
     }     }
Line 347: Line 375:
         super.onRestart();         super.onRestart();
         try{         try{
-            StarbuzzDatabaseHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this);+            StarbuzzDatabaseHelper starbuzzDatabaseHelper = 
 +                new StarbuzzDatabaseHelper(this);
             db = starbuzzDatabaseHelper.getReadableDatabase();             db = starbuzzDatabaseHelper.getReadableDatabase();
  
Line 363: Line 392:
             favoritesCursor = newCursor;             favoritesCursor = newCursor;
         } catch(SQLiteException e) {         } catch(SQLiteException e) {
-            Toast toast = Toast.makeText(this, "Database unavailable", Toast.LENGTH_SHORT);+            Toast toast = Toast.makeText(this, "Database unavailable", 
 +                Toast.LENGTH_SHORT);
             toast.show();             toast.show();
         }         }
Line 418: Line 448:
         try {         try {
             // Gain access to our app's database:             // Gain access to our app's database:
-            SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this);+            SQLiteOpenHelper starbuzzDatabaseHelper = 
 +                new StarbuzzDatabaseHelper(this);
             SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase();             SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase();
  
             // Get the name, description, and image for drinkNo:             // Get the name, description, and image for drinkNo:
             Cursor cursor = db.query ("DRINK",             Cursor cursor = db.query ("DRINK",
-                    new String[] {"NAME", "DESCRIPTION", "IMAGE_RESOURCE_ID", "FAVORITE"},+                    new String[] 
 +                        {"NAME", "DESCRIPTION", "IMAGE_RESOURCE_ID", "FAVORITE"},
                     "_id = ?",                     "_id = ?",
                     new String[] {Integer.toString(drinkNo)},                     new String[] {Integer.toString(drinkNo)},
Line 458: Line 490:
             db.close();             db.close();
         } catch (SQLiteException e) {         } catch (SQLiteException e) {
-            Toast toast = Toast.makeText(this, "Database unavailable", Toast.LENGTH_SHORT);+            Toast toast = Toast.makeText(this, "Database unavailable", 
 +                Toast.LENGTH_SHORT);
             toast.show();             toast.show();
         }         }
Line 479: Line 512:
             drinkValues = new ContentValues();             drinkValues = new ContentValues();
             drinkValues.put("FAVORITE", favorite.isChecked());             drinkValues.put("FAVORITE", favorite.isChecked());
-            Toast.makeText(DrinkActivity.this, "Backgrounding DB operation...", Toast.LENGTH_SHORT).show();+            Toast.makeText(DrinkActivity.this, "Backgrounding DB operation...",  
 +                Toast.LENGTH_SHORT).show();
         }         }
  
Line 485: Line 519:
         protected Boolean doInBackground(Integer... drinks) {         protected Boolean doInBackground(Integer... drinks) {
             int drinkNo = drinks[0];             int drinkNo = drinks[0];
-            SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(DrinkActivity.this);+            SQLiteOpenHelper starbuzzDatabaseHelper =  
 +                new StarbuzzDatabaseHelper(DrinkActivity.this);
             try {             try {
                 SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase();                 SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase();
-                db.update("DRINK", drinkValues, "_id = ?", new String[]{Integer.toString(drinkNo)});+                db.update("DRINK", drinkValues, "_id = ?", 
 +                    new String[]{Integer.toString(drinkNo)});
                 db.close();                 db.close();
                 return true;                 return true;
Line 499: Line 535:
         protected void onPostExecute(Boolean success) {         protected void onPostExecute(Boolean success) {
             if (!success) {             if (!success) {
-                Toast.makeText(DrinkActivity.this, "Database unavailable", Toast.LENGTH_SHORT).show();+                Toast.makeText(DrinkActivity.this, "Database unavailable",  
 +                    Toast.LENGTH_SHORT).show();
             } else {             } else {
-                Toast.makeText(DrinkActivity.this, "DB update complete", Toast.LENGTH_SHORT).show();+                Toast.makeText(DrinkActivity.this, "DB update complete", 
 +                    Toast.LENGTH_SHORT).show();
             }             }
         }         }
Line 509: Line 547:
 </file> </file>
  
-===== Logcat ===== +===== Debug with the system log ===== 
-<WRAP center round info 90%> +This isn't covered until Chapter 13 in the bookbut it's worth using here.
-This is not included in the book but really, really, really should be! +
-</WRAP> +
- +
-In this final version of DrinkActivity, I use the system log instead of Toasts to output debug information to the console. ''Log.d(//tag//, //message//)'' lets you output anything you want to the console. Example:<code java> +
-Log.d("MyMessages", "Hello, world."+
-</code> +
- +
-You have to import import ''android.util.Log'' to use ''Log.d''+
- +
-When you run your app in debug mode (//Run > Debug ''app''// or the bug icon in the toolbar or Shift+F9) you should see a new pane on the bottom panel: **6: Android monitor**. Your messages will show up there---along with a bunch of others. You can filter out everything except what you have tagged with your //tag// by selecting "Edit filter configuration" from dropdown at the far right of the **6: Android monitor** pane and adding your //tag// to the "Log Tag" field. (You may have to uncheck the "Regex" box.)+
  
-For more infosee [[https://developer.android.com/tools/debugging/debugging-studio.html|Debugging with Android Studio]] at the Android Developers site.+In this final version of DrinkActivityI use the [[android_learning:using_the_system_log|system log]] to output debug information to the console instead of using Toasts for debug messages.
  
 <file java DrinkActivity.java> <file java DrinkActivity.java>
Line 556: Line 584:
         try {         try {
             // Gain access to our app's database:             // Gain access to our app's database:
-            SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this);+            SQLiteOpenHelper starbuzzDatabaseHelper =  
 +                new StarbuzzDatabaseHelper(this);
             SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase();             SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase();
  
             // Get the name, description, and image for drinkNo:             // Get the name, description, and image for drinkNo:
             Cursor cursor = db.query ("DRINK",             Cursor cursor = db.query ("DRINK",
-                    new String[] {"NAME", "DESCRIPTION", "IMAGE_RESOURCE_ID", "FAVORITE"},+                    new String[] 
 +                        {"NAME", "DESCRIPTION", "IMAGE_RESOURCE_ID", "FAVORITE"},
                     "_id = ?",                     "_id = ?",
                     new String[] {Integer.toString(drinkNo)},                     new String[] {Integer.toString(drinkNo)},
Line 596: Line 626:
             db.close();             db.close();
         } catch (SQLiteException e) {         } catch (SQLiteException e) {
-            Toast toast = Toast.makeText(this, "Database unavailable", Toast.LENGTH_SHORT);+            Toast toast = Toast.makeText(this, "Database unavailable", 
 +                Toast.LENGTH_SHORT);
             toast.show();             toast.show();
         }         }
Line 623: Line 654:
         protected Boolean doInBackground(Integer... drinks) {         protected Boolean doInBackground(Integer... drinks) {
             int drinkNo = drinks[0];             int drinkNo = drinks[0];
-            SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(DrinkActivity.this);+            SQLiteOpenHelper starbuzzDatabaseHelper = 
 +                new StarbuzzDatabaseHelper(DrinkActivity.this);
             try {             try {
                 SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase();                 SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase();
-                db.update("DRINK", drinkValues, "_id = ?", new String[]{Integer.toString(drinkNo)});+                db.update("DRINK", drinkValues, "_id = ?", 
 +                    new String[]{Integer.toString(drinkNo)});
                 db.close();                 db.close();
                 return true;                 return true;
Line 637: Line 670:
         protected void onPostExecute(Boolean success) {         protected void onPostExecute(Boolean success) {
             if (!success) {             if (!success) {
-                Toast.makeText(DrinkActivity.this, "Database unavailable", Toast.LENGTH_SHORT).show();+                Toast.makeText(DrinkActivity.this, "Database unavailable", 
 +                    Toast.LENGTH_SHORT).show();
             } else {             } else {
                 Log.d("DB", "DB update complete");                 Log.d("DB", "DB update complete");
android_learning/headfirst_android_development_notes/chapter_12.1461468308.txt.gz · Last modified: 2016/04/24 03:25 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki