package com.hfad.starbuzz; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteOpenHelper; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; 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.getReadableDatabase(); // Get the name, description, and image for drinkNo: Cursor cursor = db.query ("DRINK", new String[] {"NAME", "DESCRIPTION", "IMAGE_RESOURCE_ID"}, "_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); // 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); } // Close up shop. cursor.close(); db.close(); } catch (SQLiteException e) { Toast toast = Toast.makeText(this, "Database unavailable", Toast.LENGTH_SHORT); toast.show(); } } }