android_learning:headfirst_android_development_notes:chapter_12
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
android_learning:headfirst_android_development_notes:chapter_12 [2016/04/24 02:31] – [pp. 526+: Threads] mithat | android_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. | + | |
- | </ | + | 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 = |
+ | | ||
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, | + | Toast toast = Toast.makeText(this, |
+ | | ||
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(); | + | ListView listDrinks = getListView(); |
+ | // | ||
+ | // | ||
// 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 = |
+ | | ||
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, | + | Toast.makeText(this, |
+ | | ||
} | } | ||
} | } | ||
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, | super.onListItemClick(l, | ||
- | Intent intent = new Intent(DrinkCategoryActivity.this, | + | Intent intent = new Intent(DrinkCategoryActivity.this, |
+ | | ||
intent.putExtra(DrinkActivity.EXTRA_DRINKNO, | intent.putExtra(DrinkActivity.EXTRA_DRINKNO, | ||
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 = |
+ | | ||
SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase(); | SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase(); | ||
// Get the name, description, | // Get the name, description, | ||
Cursor cursor = db.query (" | Cursor cursor = db.query (" | ||
- | new String[] {" | + | new String[] |
+ | | ||
"_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, | + | Toast toast = Toast.makeText(this, |
+ | | ||
toast.show(); | toast.show(); | ||
} | } | ||
Line 239: | Line 253: | ||
drinkValues.put(" | drinkValues.put(" | ||
- | SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(DrinkActivity.this); | + | SQLiteOpenHelper starbuzzDatabaseHelper = |
+ | | ||
try { | try { | ||
SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase(); | SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase(); | ||
- | db.update(" | + | db.update(" |
+ | | ||
db.close(); | db.close(); | ||
} catch(SQLiteException e) { | } catch(SQLiteException e) { | ||
- | Toast.makeText(DrinkActivity.this, | + | Toast.makeText(DrinkActivity.this, |
+ | | ||
} | } | ||
} | } | ||
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 = |
+ | | ||
@Override | @Override | ||
- | public void onItemClick(AdapterView<?> | + | public void onItemClick (AdapterView<?> |
- | if (position == 0) { | + | |
- | Intent intent = new Intent(TopLevelActivity.this, | + | if (position == 0) { |
- | startActivity(intent); | + | Intent intent = new Intent(TopLevelActivity.this, |
- | } | + | |
+ | 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 = |
+ | | ||
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( |
+ | | ||
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( |
+ | | ||
@Override | @Override | ||
- | public void onItemClick(AdapterView<?> | + | public void onItemClick( |
- | Intent intent = new Intent(TopLevelActivity.this, | + | |
- | intent.putExtra(DrinkActivity.EXTRA_DRINKNO, | + | Intent intent = |
- | startActivity(intent); | + | |
+ | intent.putExtra(DrinkActivity.EXTRA_DRINKNO, | ||
+ | startActivity(intent); | ||
+ | } | ||
} | } | ||
- | | + | ); |
} catch(SQLiteException e) { | } catch(SQLiteException e) { | ||
- | Toast.makeText(TopLevelActivity.this, | + | Toast.makeText(TopLevelActivity.this, |
+ | | ||
} | } | ||
} | } | ||
Line 347: | Line 375: | ||
super.onRestart(); | super.onRestart(); | ||
try{ | try{ | ||
- | StarbuzzDatabaseHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this); | + | StarbuzzDatabaseHelper starbuzzDatabaseHelper = |
+ | | ||
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, | + | Toast toast = Toast.makeText(this, |
+ | | ||
toast.show(); | toast.show(); | ||
} | } | ||
Line 381: | Line 411: | ||
In creating subclasses of AsycTask, you must override the '' | In creating subclasses of AsycTask, you must override the '' | ||
- | ===== P. 530 ===== | + | ===== p. 530 ===== |
- | Note that the UpdateDrinkTask will be added as an Inner Class to DrinkActivity. So don't create a new '' | + | The UpdateDrinkTask |
+ | ===== pp. 537-538 ===== | ||
+ | I've added some Toasts to mark when some things are happening. You wouldn' | ||
+ | <file java DrinkActivity.java> | ||
+ | package com.hfad.starbuzz; | ||
+ | import android.content.ContentValues; | ||
+ | import android.database.Cursor; | ||
+ | import android.database.sqlite.SQLiteDatabase; | ||
+ | import android.database.sqlite.SQLiteException; | ||
+ | import android.database.sqlite.SQLiteOpenHelper; | ||
+ | import android.os.AsyncTask; | ||
+ | import android.support.v7.app.AppCompatActivity; | ||
+ | import android.os.Bundle; | ||
+ | import android.view.View; | ||
+ | import android.widget.CheckBox; | ||
+ | import android.widget.ImageView; | ||
+ | import android.widget.TextView; | ||
+ | import android.widget.Toast; | ||
+ | |||
+ | public class DrinkActivity extends AppCompatActivity { | ||
+ | |||
+ | public static final String EXTRA_DRINKNO = " | ||
+ | |||
+ | @Override | ||
+ | protected void onCreate(Bundle savedInstanceState) { | ||
+ | super.onCreate(savedInstanceState); | ||
+ | setContentView(R.layout.activity_drink); | ||
+ | |||
+ | int drinkNo = (Integer)getIntent().getExtras().get(EXTRA_DRINKNO); | ||
+ | |||
+ | //Create a cursor and populate Views from the cursor' | ||
+ | try { | ||
+ | // Gain access to our app's database: | ||
+ | SQLiteOpenHelper starbuzzDatabaseHelper = | ||
+ | new StarbuzzDatabaseHelper(this); | ||
+ | SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase(); | ||
+ | |||
+ | // Get the name, description, | ||
+ | Cursor cursor = db.query (" | ||
+ | new String[] | ||
+ | {" | ||
+ | "_id = ?", | ||
+ | new String[] {Integer.toString(drinkNo)}, | ||
+ | null, null,null); | ||
+ | |||
+ | // Move to the first record in the Cursor | ||
+ | if (cursor.moveToFirst()) { | ||
+ | // Get the drink details from the cursor | ||
+ | String nameText = cursor.getString(0); | ||
+ | String descriptionText = cursor.getString(1); | ||
+ | int photoId = cursor.getInt(2); | ||
+ | boolean isFavorite = (cursor.getInt(3) == 1); | ||
+ | |||
+ | // Populate the drink name | ||
+ | TextView name = (TextView)findViewById(R.id.name); | ||
+ | name.setText(nameText); | ||
+ | |||
+ | // Populate the drink description | ||
+ | TextView description = (TextView)findViewById(R.id.description); | ||
+ | description.setText(descriptionText); | ||
+ | |||
+ | // Populate the drink image | ||
+ | ImageView photo = (ImageView)findViewById(R.id.photo); | ||
+ | photo.setImageResource(photoId); | ||
+ | photo.setContentDescription(nameText); | ||
+ | |||
+ | // Populate the favorite checkbox | ||
+ | CheckBox favorite = (CheckBox)findViewById(R.id.favorite); | ||
+ | favorite.setChecked(isFavorite); | ||
+ | } | ||
+ | |||
+ | // Close up shop. | ||
+ | cursor.close(); | ||
+ | db.close(); | ||
+ | } catch (SQLiteException e) { | ||
+ | Toast toast = Toast.makeText(this, | ||
+ | Toast.LENGTH_SHORT); | ||
+ | toast.show(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // Update the DB's FAVORITES field for this drink. | ||
+ | public void onFavoriteClicked(View view) { | ||
+ | int drinkNo = (Integer)getIntent().getExtras().get(" | ||
+ | new UpdateDrinkTask().execute(drinkNo); | ||
+ | } | ||
+ | |||
+ | // Inner class: a task to update the drink' | ||
+ | private class UpdateDrinkTask extends AsyncTask < | ||
+ | |||
+ | ContentValues drinkValues; | ||
+ | |||
+ | @Override | ||
+ | protected void onPreExecute() { | ||
+ | CheckBox favorite = (CheckBox)findViewById(R.id.favorite); | ||
+ | drinkValues = new ContentValues(); | ||
+ | drinkValues.put(" | ||
+ | Toast.makeText(DrinkActivity.this, | ||
+ | Toast.LENGTH_SHORT).show(); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected Boolean doInBackground(Integer... drinks) { | ||
+ | int drinkNo = drinks[0]; | ||
+ | SQLiteOpenHelper starbuzzDatabaseHelper = | ||
+ | new StarbuzzDatabaseHelper(DrinkActivity.this); | ||
+ | try { | ||
+ | SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase(); | ||
+ | db.update(" | ||
+ | new String[]{Integer.toString(drinkNo)}); | ||
+ | db.close(); | ||
+ | return true; | ||
+ | } catch(SQLiteException e) { | ||
+ | return false; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected void onPostExecute(Boolean success) { | ||
+ | if (!success) { | ||
+ | Toast.makeText(DrinkActivity.this, | ||
+ | Toast.LENGTH_SHORT).show(); | ||
+ | } else { | ||
+ | Toast.makeText(DrinkActivity.this, | ||
+ | Toast.LENGTH_SHORT).show(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== Debug with the system log ===== | ||
+ | This isn't covered until Chapter 13 in the book, but it's worth using here. | ||
+ | |||
+ | In this final version of DrinkActivity, | ||
+ | |||
+ | <file java DrinkActivity.java> | ||
+ | package com.hfad.starbuzz; | ||
+ | |||
+ | import android.content.ContentValues; | ||
+ | import android.database.Cursor; | ||
+ | import android.database.sqlite.SQLiteDatabase; | ||
+ | import android.database.sqlite.SQLiteException; | ||
+ | import android.database.sqlite.SQLiteOpenHelper; | ||
+ | import android.os.AsyncTask; | ||
+ | import android.support.v7.app.AppCompatActivity; | ||
+ | import android.os.Bundle; | ||
+ | import android.util.Log; | ||
+ | import android.view.View; | ||
+ | import android.widget.CheckBox; | ||
+ | import android.widget.ImageView; | ||
+ | import android.widget.TextView; | ||
+ | import android.widget.Toast; | ||
+ | |||
+ | public class DrinkActivity extends AppCompatActivity { | ||
+ | |||
+ | public static final String EXTRA_DRINKNO = " | ||
+ | |||
+ | @Override | ||
+ | protected void onCreate(Bundle savedInstanceState) { | ||
+ | super.onCreate(savedInstanceState); | ||
+ | setContentView(R.layout.activity_drink); | ||
+ | |||
+ | int drinkNo = (Integer)getIntent().getExtras().get(EXTRA_DRINKNO); | ||
+ | |||
+ | //Create a cursor and populate Views from the cursor' | ||
+ | try { | ||
+ | // Gain access to our app's database: | ||
+ | SQLiteOpenHelper starbuzzDatabaseHelper = | ||
+ | new StarbuzzDatabaseHelper(this); | ||
+ | SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase(); | ||
+ | |||
+ | // Get the name, description, | ||
+ | Cursor cursor = db.query (" | ||
+ | new String[] | ||
+ | {" | ||
+ | "_id = ?", | ||
+ | new String[] {Integer.toString(drinkNo)}, | ||
+ | null, null,null); | ||
+ | |||
+ | // Move to the first record in the Cursor | ||
+ | if (cursor.moveToFirst()) { | ||
+ | // Get the drink details from the cursor | ||
+ | String nameText = cursor.getString(0); | ||
+ | String descriptionText = cursor.getString(1); | ||
+ | int photoId = cursor.getInt(2); | ||
+ | boolean isFavorite = (cursor.getInt(3) == 1); | ||
+ | |||
+ | // Populate the drink name | ||
+ | TextView name = (TextView)findViewById(R.id.name); | ||
+ | name.setText(nameText); | ||
+ | |||
+ | // Populate the drink description | ||
+ | TextView description = (TextView)findViewById(R.id.description); | ||
+ | description.setText(descriptionText); | ||
+ | |||
+ | // Populate the drink image | ||
+ | ImageView photo = (ImageView)findViewById(R.id.photo); | ||
+ | photo.setImageResource(photoId); | ||
+ | photo.setContentDescription(nameText); | ||
+ | |||
+ | // Populate the favorite checkbox | ||
+ | CheckBox favorite = (CheckBox)findViewById(R.id.favorite); | ||
+ | favorite.setChecked(isFavorite); | ||
+ | } | ||
+ | |||
+ | // Close up shop. | ||
+ | cursor.close(); | ||
+ | db.close(); | ||
+ | } catch (SQLiteException e) { | ||
+ | Toast toast = Toast.makeText(this, | ||
+ | Toast.LENGTH_SHORT); | ||
+ | toast.show(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // Update the DB's FAVORITES field for this drink. | ||
+ | public void onFavoriteClicked(View view) { | ||
+ | int drinkNo = (Integer)getIntent().getExtras().get(" | ||
+ | new UpdateDrinkTask().execute(drinkNo); | ||
+ | } | ||
+ | |||
+ | // Inner class: a task to update the drink' | ||
+ | private class UpdateDrinkTask extends AsyncTask < | ||
+ | |||
+ | ContentValues drinkValues; | ||
+ | |||
+ | @Override | ||
+ | protected void onPreExecute() { | ||
+ | CheckBox favorite = (CheckBox)findViewById(R.id.favorite); | ||
+ | drinkValues = new ContentValues(); | ||
+ | drinkValues.put(" | ||
+ | Log.d(" | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected Boolean doInBackground(Integer... drinks) { | ||
+ | int drinkNo = drinks[0]; | ||
+ | SQLiteOpenHelper starbuzzDatabaseHelper = | ||
+ | new StarbuzzDatabaseHelper(DrinkActivity.this); | ||
+ | try { | ||
+ | SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase(); | ||
+ | db.update(" | ||
+ | new String[]{Integer.toString(drinkNo)}); | ||
+ | db.close(); | ||
+ | return true; | ||
+ | } catch(SQLiteException e) { | ||
+ | return false; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected void onPostExecute(Boolean success) { | ||
+ | if (!success) { | ||
+ | Toast.makeText(DrinkActivity.this, | ||
+ | Toast.LENGTH_SHORT).show(); | ||
+ | } else { | ||
+ | Log.d(" | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ |
android_learning/headfirst_android_development_notes/chapter_12.1461465099.txt.gz · Last modified: 2016/04/24 02:31 by mithat