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 = "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's data. try { // Gain access to our app's database: SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this); SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase(); // Get the name, description, and image for drinkNo: Cursor cursor = db.query ("DRINK", new String[] {"NAME", "DESCRIPTION", "IMAGE_RESOURCE_ID", "FAVORITE"}, "_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, "Database unavailable", 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("drinkNo"); new UpdateDrinkTask().execute(drinkNo); } // Inner class: a task to update the drink's DB record. private class UpdateDrinkTask extends AsyncTask { ContentValues drinkValues; @Override protected void onPreExecute() { CheckBox favorite = (CheckBox)findViewById(R.id.favorite); drinkValues = new ContentValues(); drinkValues.put("FAVORITE", favorite.isChecked()); Toast.makeText(DrinkActivity.this, "Backgrounding DB operation...", 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("DRINK", drinkValues, "_id = ?", 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, "Database unavailable", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(DrinkActivity.this, "DB update complete", Toast.LENGTH_SHORT).show(); } } } }