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 03:44] – 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 ====== | ||
- | 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. Kludgey for sure, but it gets the job done. \\ | + | 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. |
+ | |||
+ | Kludgey for sure, but it gets the job done. \\ | ||
| | ||
| | ||
Line 200: | Line 202: | ||
// 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 300: | 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 317: | 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 327: | 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 336: | 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 366: | Line 375: | ||
super.onRestart(); | super.onRestart(); | ||
try{ | try{ | ||
- | StarbuzzDatabaseHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this); | + | StarbuzzDatabaseHelper starbuzzDatabaseHelper = |
+ | | ||
db = starbuzzDatabaseHelper.getReadableDatabase(); | db = starbuzzDatabaseHelper.getReadableDatabase(); | ||
Line 382: | 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 437: | 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 = |
+ | | ||
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 477: | Line 490: | ||
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 498: | Line 512: | ||
drinkValues = new ContentValues(); | drinkValues = new ContentValues(); | ||
drinkValues.put(" | drinkValues.put(" | ||
- | Toast.makeText(DrinkActivity.this, | + | Toast.makeText(DrinkActivity.this, |
+ | | ||
} | } | ||
Line 504: | 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 = |
+ | | ||
try { | try { | ||
SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase(); | SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase(); | ||
- | db.update(" | + | db.update(" |
+ | | ||
db.close(); | db.close(); | ||
return true; | return true; | ||
Line 518: | Line 535: | ||
protected void onPostExecute(Boolean success) { | protected void onPostExecute(Boolean success) { | ||
if (!success) { | if (!success) { | ||
- | Toast.makeText(DrinkActivity.this, | + | Toast.makeText(DrinkActivity.this, |
+ | | ||
} else { | } else { | ||
- | Toast.makeText(DrinkActivity.this, | + | Toast.makeText(DrinkActivity.this, |
+ | | ||
} | } | ||
} | } | ||
Line 528: | Line 547: | ||
</ | </ | ||
- | ===== Logcat | + | ===== Debug with the system log ===== |
This isn't covered until Chapter 13 in the book, but it's worth using here. | This isn't covered until Chapter 13 in the book, but it's worth using here. | ||
Line 565: | 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 = |
+ | | ||
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 605: | Line 626: | ||
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 632: | 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 = |
+ | | ||
try { | try { | ||
SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase(); | SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase(); | ||
- | db.update(" | + | db.update(" |
+ | | ||
db.close(); | db.close(); | ||
return true; | return true; | ||
Line 646: | Line 670: | ||
protected void onPostExecute(Boolean success) { | protected void onPostExecute(Boolean success) { | ||
if (!success) { | if (!success) { | ||
- | Toast.makeText(DrinkActivity.this, | + | Toast.makeText(DrinkActivity.this, |
+ | | ||
} else { | } else { | ||
Log.d(" | Log.d(" |
android_learning/headfirst_android_development_notes/chapter_12.1461469456.txt.gz · Last modified: 2016/04/24 03:44 by mithat