package com.hfad.starbuzz; import android.app.ListActivity; import android.content.Intent; 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.view.View; import android.widget.ArrayAdapter; import android.widget.CursorAdapter; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.Toast; public class DrinkCategoryActivity extends ListActivity { private SQLiteDatabase db; // the app's db private Cursor cursor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ListView listDrinks = getListView(); // i.e., this.getListView(); // get the ListView used by // this ListActivity // Use a cursor via an adapter to populate listDrinks try { // Gain access to our app's database: SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this); db = starbuzzDatabaseHelper.getReadableDatabase(); // Set a cursor on the _id and name: Cursor cursor = db.query ("DRINK", new String[] {"_id", "NAME"}, null, null, null, null,null); // Create a cursor adapter for the cursor CursorAdapter listAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, new String[]{"NAME"}, new int[]{android.R.id.text1}, 0); // Set listDrinks' adapter: listDrinks.setAdapter(listAdapter); } catch (SQLiteException e) { Toast.makeText(this, "Database unavailable", Toast.LENGTH_SHORT).show(); } } @Override protected void onDestroy() { super.onDestroy(); if (cursor != null) {cursor.close();} if (db != null) {db.close();} } // Start new activity when an item is clicked @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Intent intent = new Intent(DrinkCategoryActivity.this, DrinkActivity.class); intent.putExtra(DrinkActivity.EXTRA_DRINKNO, (int) id); startActivity(intent); } }