| @@ -0,0 +1,86 @@ | ||
| package com.example.sixth; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import android.app.Activity; | ||
| import android.os.Bundle; | ||
| import android.widget.ExpandableListView; | ||
|
|
||
|
|
||
| public class Drinks extends Activity { | ||
| // TextView drinkHolder; | ||
| String barName; | ||
| //static String barNameHolder = MainActivity.upperCaseName; | ||
| String drinkTestHolder = ""; | ||
| ExpandableListAdapter listAdapter; | ||
| ExpandableListView expListView; | ||
| List<String> listDataHeader; | ||
| HashMap<String, List<String>> listDataChild; | ||
| List<String> beer; | ||
| //List<String> beerLables = MainActivity.beerLables; | ||
|
|
||
|
|
||
| @Override | ||
| public void onCreate(Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
| setContentView(R.layout.activity_drinks); | ||
| Bundle bundle = getIntent().getExtras(); | ||
| //if (bundle != null) { | ||
| // barNameHolder = bundle.getString("bar"); | ||
| // barName =barNameHolder; | ||
| //} | ||
| // get the listview | ||
|
|
||
| expListView = (ExpandableListView) findViewById(R.id.lvExp); | ||
|
|
||
| // preparing list data | ||
| prepareListData(); | ||
|
|
||
| listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); | ||
|
|
||
| // setting list adapter | ||
| expListView.setAdapter(listAdapter); | ||
|
|
||
| } | ||
|
|
||
| private void prepareListData() { | ||
| listDataHeader = new ArrayList<String>(); | ||
| listDataChild = new HashMap<String, List<String>>(); | ||
|
|
||
|
|
||
| // Adding child data | ||
|
|
||
| listDataHeader.add("Beer"); | ||
| listDataHeader.add("Mixed Drinks"); | ||
| listDataHeader.add("Other Drinks"); | ||
| listDataHeader.add("Shots"); | ||
| listDataHeader.add("Wine"); | ||
|
|
||
| DBHelper db = new DBHelper(getApplicationContext()); | ||
| List<String> beerLables = db.getBeerDrinkLabels(); | ||
| for (int i = 0; i < beerLables.size()+1; i++) { | ||
| beer.add(beerLables.get(i)); | ||
| } | ||
|
|
||
|
|
||
| //beer.add("The Shawshank Redemption"); | ||
| List<String> wine = new ArrayList<String>(); | ||
|
|
||
|
|
||
| List<String> shots = new ArrayList<String>(); | ||
|
|
||
|
|
||
| List<String> mixedDrinks = new ArrayList<String>(); | ||
|
|
||
|
|
||
| List<String> otherDrinks = new ArrayList<String>(); | ||
|
|
||
|
|
||
| listDataChild.put(listDataHeader.get(0), beer); // Header, Child data | ||
| listDataChild.put(listDataHeader.get(1), wine); | ||
| listDataChild.put(listDataHeader.get(2), shots); | ||
| listDataChild.put(listDataHeader.get(3), mixedDrinks); | ||
| listDataChild.put(listDataHeader.get(4), otherDrinks); | ||
| } | ||
| } |
| @@ -0,0 +1,133 @@ | ||
| package com.example.sixth; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import android.app.Activity; | ||
| import android.content.Intent; | ||
| import android.database.SQLException; | ||
| import android.os.Bundle; | ||
| import android.view.Menu; | ||
| import android.view.MenuItem; | ||
| import android.view.View; | ||
| import android.view.View.OnClickListener; | ||
| import android.widget.AdapterView; | ||
| import android.widget.AdapterView.OnItemSelectedListener; | ||
| import android.widget.ArrayAdapter; | ||
| import android.widget.Button; | ||
| import android.widget.Spinner; | ||
|
|
||
|
|
||
| public class MainActivity extends Activity implements | ||
| OnItemSelectedListener { | ||
| DBHelper myDB; | ||
| Button selectBar; | ||
| Spinner spinner; | ||
| static String pullBar,setBar,name, cityState; | ||
| public String upperCaseName; | ||
|
|
||
| // static List<String> beer, beerLables; | ||
|
|
||
| @Override | ||
| public void onCreate(Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
| setContentView(R.layout.activity_main); | ||
| Button selectBar = (Button)findViewById(R.id.btnSelectBar); | ||
| myDB = new DBHelper(this); | ||
| // Spinner element | ||
| spinner = (Spinner) findViewById(R.id.spinner); | ||
| // Spinner click listener | ||
| spinner.setOnItemSelectedListener(this); | ||
|
|
||
| try { | ||
| myDB.createDataBase(); | ||
| } catch (IOException ioe) { | ||
| throw new Error("Unable to create database"); | ||
| } | ||
| try { | ||
| myDB.openDataBase(); | ||
| } catch (SQLException sqle) { | ||
| throw sqle; | ||
| } | ||
|
|
||
|
|
||
| // Loading spinner data from database | ||
| loadSpinnerData(); | ||
|
|
||
| selectBar.setOnClickListener(new OnClickListener() { | ||
| @Override | ||
| public void onClick(View v) { | ||
| pullBar = String.valueOf(spinner.getSelectedItem()); | ||
| String[] parts = pullBar.split(" "); //returns an array with the 2 parts | ||
| name = parts[0]; | ||
|
|
||
| String nlast = parts[parts.length-2]; | ||
| String last = parts[parts.length-1]; | ||
|
|
||
| cityState = nlast+ " " + last; | ||
| upperCaseName=name.toUpperCase(); | ||
| setBar = name.toLowerCase(); | ||
|
|
||
|
|
||
| DBHelper.barTableName = upperCaseName; | ||
| Intent i = (new Intent(MainActivity.this, Bar.class)); | ||
| //i.putExtra("bar",upperCaseName); | ||
| startActivity(i); | ||
| } | ||
|
|
||
| }); | ||
| } | ||
|
|
||
|
|
||
| private void loadSpinnerData() { | ||
| // database handler | ||
| DBHelper db = new DBHelper(getApplicationContext()); | ||
|
|
||
| // Spinner Drop down elements | ||
| List<String> lables = db.getAllLabels(); | ||
|
|
||
| // Creating adapter for spinner | ||
| ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, | ||
| android.R.layout.simple_spinner_item, lables); | ||
|
|
||
| // Drop down layout style - list view with radio button | ||
| dataAdapter | ||
| .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | ||
|
|
||
| // attaching data adapter to spinner | ||
| spinner.setAdapter(dataAdapter); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean onCreateOptionsMenu(Menu menu) { | ||
| // Inflate the menu; this adds items to the action bar if it is present. | ||
| getMenuInflater().inflate(R.menu.main, menu); | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean onOptionsItemSelected(MenuItem item) { | ||
| // Handle action bar item clicks here. The action bar will | ||
| // automatically handle clicks on the Home/Up button, so long | ||
| // as you specify a parent activity in AndroidManifest.xml. | ||
| int id = item.getItemId(); | ||
| if (id == R.id.action_settings) { | ||
| return true; | ||
| } | ||
| return super.onOptionsItemSelected(item); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { | ||
| // TODO Auto-generated method stub | ||
|
|
||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public void onNothingSelected(AdapterView<?> parent) { | ||
| // TODO Auto-generated method stub | ||
|
|
||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,195 @@ | ||
| package com.example.sixth; | ||
|
|
||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import android.content.Context; | ||
| import android.database.Cursor; | ||
| import android.database.SQLException; | ||
| import android.database.sqlite.SQLiteDatabase; | ||
| import android.database.sqlite.SQLiteException; | ||
| import android.database.sqlite.SQLiteOpenHelper; | ||
|
|
||
| public class DBHelper extends SQLiteOpenHelper { | ||
|
|
||
| private static String DB_PATH = "/data/data/com.example.sixth/databases/"; | ||
| private static String DB_NAME = "BarSample.db"; | ||
| private final Context myContext; | ||
| public static String tableName = "BARS"; | ||
| String barTableName, sqlquery; | ||
| //String firstBarTableName = MainActivity.upperCaseName;// Pass in the | ||
| // specific | ||
| // bar from | ||
| // the | ||
| // spinner | ||
| // choice | ||
| private SQLiteDatabase myDataBase; | ||
|
|
||
| public DBHelper(Context context) { | ||
|
|
||
| super(context, DB_NAME, null, 1); | ||
| this.myContext = context; | ||
| } | ||
|
|
||
|
|
||
| //Creates a empty database on the system and rewrites it with your own | ||
| // database. | ||
|
|
||
| public void createDataBase() throws IOException { | ||
|
|
||
| boolean dbExist = checkDataBase(); | ||
|
|
||
| if (dbExist) { | ||
| // do nothing - database already exist | ||
| } else { | ||
|
|
||
| // By calling this method and empty database will be created into | ||
| // the default system path | ||
| // of your application so we are gonna be able to overwrite that | ||
| // database with our database. | ||
| this.getReadableDatabase(); | ||
|
|
||
| try { | ||
| this.close(); | ||
| copyDataBase(); | ||
|
|
||
| } catch (IOException e) { | ||
|
|
||
| throw new Error("Error copying database"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| //Check if the database already exist to avoid re-copying the file each | ||
| // time you open the application. | ||
|
|
||
| // @return true if it exists, false if it doesn't | ||
|
|
||
| private boolean checkDataBase() { | ||
|
|
||
| SQLiteDatabase checkDB = null; | ||
|
|
||
| try { | ||
| String myPath = DB_PATH + DB_NAME; | ||
| checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | ||
|
|
||
| } catch (SQLiteException e) { | ||
| // database does't exist yet. | ||
| } | ||
| if (checkDB != null) { | ||
| checkDB.close(); | ||
| } | ||
| return checkDB != null ? true : false; | ||
| } | ||
|
|
||
|
|
||
| //Copies your database from your local assets-folder to the just created | ||
| //empty database in the system folder, from where it can be accessed and | ||
| // handled. This is done by transfering bytestream. | ||
|
|
||
| private void copyDataBase() throws IOException { | ||
|
|
||
| // Open your local db as the input stream | ||
| InputStream myInput = myContext.getAssets().open(DB_NAME); | ||
|
|
||
| // Path to the just created empty db | ||
| String outFileName = DB_PATH + DB_NAME; | ||
|
|
||
| // Open the empty db as the output stream | ||
| OutputStream myOutput = new FileOutputStream(outFileName); | ||
|
|
||
| // transfer bytes from the inputfile to the outputfile | ||
| byte[] buffer = new byte[1024]; | ||
| int length; | ||
| while ((length = myInput.read(buffer)) > 0) { | ||
| myOutput.write(buffer, 0, length); | ||
| } | ||
|
|
||
| // Close the streams | ||
| myOutput.flush(); | ||
| myOutput.close(); | ||
| myInput.close(); | ||
|
|
||
| } | ||
|
|
||
| public void openDataBase() throws SQLException { | ||
|
|
||
| // Open the database | ||
| String myPath = DB_PATH + DB_NAME; | ||
| myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public synchronized void close() { | ||
|
|
||
| if (myDataBase != null) | ||
| myDataBase.close(); | ||
|
|
||
| super.close(); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onCreate(SQLiteDatabase db) { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | ||
|
|
||
| } | ||
|
|
||
| public List<String> getAllLabels() { | ||
| List<String> labels = new ArrayList<String>(); | ||
|
|
||
| // Select All Query | ||
| String selectQuery = "SELECT FROM " + tableName; | ||
|
|
||
| SQLiteDatabase db = this.getReadableDatabase(); | ||
| Cursor cursor = db.rawQuery(selectQuery, null); | ||
|
|
||
| // looping through all rows and adding to list | ||
| if (cursor.moveToFirst()) { | ||
| do { | ||
| labels.add(cursor.getString(1) + " " + cursor.getString(2) + ", " + cursor.getString(3)); | ||
| } while (cursor.moveToNext()); | ||
| } | ||
|
|
||
| // closing connection | ||
| cursor.close(); | ||
| db.close(); | ||
|
|
||
| // returning labels | ||
| return labels; | ||
|
|
||
| } // will returns all labels stored in database | ||
| public List<String> getBeerDrinkLabels(){ | ||
|
|
||
| List<String> allBeerDrinkLabels = new ArrayList<String>(); | ||
|
|
||
| String sqlquery="SELECT FROM " + barTableName + " WHERE DRINKTYPE='Beer';"; | ||
| String selectQuery = sqlquery; | ||
|
|
||
| SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = | ||
| db.rawQuery(selectQuery, null); | ||
|
|
||
| // looping through all rows and adding to list if (cursor.moveToFirst()) | ||
| { do { allBeerDrinkLabels.add(cursor.getString(1) + " Price: " + | ||
| cursor.getString(2)); } while (cursor.moveToNext()); } | ||
|
|
||
| // closing connection | ||
| cursor.close(); | ||
| db.close(); | ||
|
|
||
| // returning labels | ||
| return allBeerDrinkLabels; | ||
|
|
||
| } // will returns all labels stored in database | ||
|
|
||
| } |
| @@ -0,0 +1,112 @@ | ||
| package com.example.sixth; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import android.app.Activity; | ||
| import android.os.Bundle; | ||
| import android.widget.ExpandableListView; | ||
|
|
||
|
|
||
| public class Drinks extends Activity { | ||
| // TextView drinkHolder; | ||
| String barName; | ||
| //static String barNameHolder = MainActivity.upperCaseName; | ||
| String drinkTestHolder = ""; | ||
| ExpandableListAdapter listAdapter; | ||
| ExpandableListView expListView; | ||
| List<String> listDataHeader; | ||
| HashMap<String, List<String>> listDataChild; | ||
| List<String> beer; | ||
| //List<String> beerLables = MainActivity.beerLables; | ||
|
|
||
|
|
||
| @Override | ||
| public void onCreate(Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
| setContentView(R.layout.activity_drinks); | ||
| Bundle bundle = getIntent().getExtras(); | ||
| //if (bundle != null) { | ||
| // barNameHolder = bundle.getString("bar"); | ||
| // barName =barNameHolder; | ||
| //} | ||
| // get the listview | ||
|
|
||
| expListView = (ExpandableListView) findViewById(R.id.lvExp); | ||
|
|
||
| // preparing list data | ||
| prepareListData(); | ||
|
|
||
| listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); | ||
|
|
||
| // setting list adapter | ||
| expListView.setAdapter(listAdapter); | ||
|
|
||
| } | ||
|
|
||
| private void prepareListData() { | ||
| listDataHeader = new ArrayList<String>(); | ||
| listDataChild = new HashMap<String, List<String>>(); | ||
|
|
||
|
|
||
| // Adding child data | ||
|
|
||
| listDataHeader.add("Beer"); | ||
| listDataHeader.add("Mixed Drinks"); | ||
| listDataHeader.add("Other Drinks"); | ||
| listDataHeader.add("Shots"); | ||
| listDataHeader.add("Wine"); | ||
|
|
||
| DBHelper db = new DBHelper(getApplicationContext()); | ||
| List<String> beerLables = db.getBeerDrinkLabels(); | ||
| for (int i = 0; i < beerLables.size()-1; i++) { | ||
| beer.add(beerLables.get(i)); | ||
| } | ||
|
|
||
| /*if(barNameHolder.equals("CHANGOS")){ | ||
| List<String> beerLables ; | ||
| List<String> beer = new ArrayList<String>();; | ||
| beerLables = db.getChangosBeerDrinkLabels(); | ||
| for (int i = 0; i < beerLables.size(); i++) { | ||
| beer.add(beerLables.get(i)); | ||
| } | ||
| } | ||
| else if (barNameHolder.equals("LANDOS")){ | ||
| List<String> beerLables; | ||
| List<String> beer = new ArrayList<String>();; | ||
| beerLables = db.getLandosBeerDrinkLabels(); | ||
| for (int i = 0; i < beerLables.size(); i++) { | ||
| beer.add(beerLables.get(i)); | ||
| } | ||
| } | ||
| else { | ||
| List<String> beerLables; | ||
| List<String> beer = new ArrayList<String>();; | ||
| beerLables = db.getAnthonysBeerDrinkLabels(); | ||
| for (int i = 0; i < beerLables.size(); i++) { | ||
| beer.add(beerLables.get(i)); | ||
| } | ||
| }*/ | ||
| //Get Beer entries | ||
| // Adding child data | ||
|
|
||
| //beer.add("The Shawshank Redemption"); | ||
| List<String> wine = new ArrayList<String>(); | ||
|
|
||
|
|
||
| List<String> shots = new ArrayList<String>(); | ||
|
|
||
|
|
||
| List<String> mixedDrinks = new ArrayList<String>(); | ||
|
|
||
|
|
||
| List<String> otherDrinks = new ArrayList<String>(); | ||
|
|
||
|
|
||
| listDataChild.put(listDataHeader.get(0), beer); // Header, Child data | ||
| listDataChild.put(listDataHeader.get(1), wine); | ||
| listDataChild.put(listDataHeader.get(2), shots); | ||
| listDataChild.put(listDataHeader.get(3), mixedDrinks); | ||
| listDataChild.put(listDataHeader.get(4), otherDrinks); | ||
| } | ||
| } |
| @@ -0,0 +1,192 @@ | ||
| package com.example.sixth; | ||
|
|
||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import android.content.Context; | ||
| import android.database.Cursor; | ||
| import android.database.SQLException; | ||
| import android.database.sqlite.SQLiteDatabase; | ||
| import android.database.sqlite.SQLiteException; | ||
| import android.database.sqlite.SQLiteOpenHelper; | ||
|
|
||
| public class DBHelper extends SQLiteOpenHelper { | ||
|
|
||
| private static String DB_PATH = "/data/data/com.example.sixth/databases/"; | ||
| private static String DB_NAME = "BarSample.db"; | ||
| private final Context myContext; | ||
| public static String tableName = "BARS"; | ||
| String barTableName, sqlquery; | ||
| //String firstBarTableName = MainActivity.upperCaseName;// Pass in the | ||
| // specific | ||
| // bar from | ||
| // the | ||
| // spinner | ||
| // choice | ||
| private SQLiteDatabase myDataBase; | ||
|
|
||
| public DBHelper(Context context) { | ||
|
|
||
| super(context, DB_NAME, null, 1); | ||
| this.myContext = context; | ||
| } | ||
|
|
||
| / | ||
| Creates a empty database on the system and rewrites it with your own | ||
| database. | ||
| / | ||
| public void createDataBase() throws IOException { | ||
|
|
||
| boolean dbExist = checkDataBase(); | ||
|
|
||
| if (dbExist) { | ||
| // do nothing - database already exist | ||
| } else { | ||
|
|
||
| // By calling this method and empty database will be created into | ||
| // the default system path | ||
| // of your application so we are gonna be able to overwrite that | ||
| // database with our database. | ||
| this.getReadableDatabase(); | ||
|
|
||
| try { | ||
| this.close(); | ||
| copyDataBase(); | ||
|
|
||
| } catch (IOException e) { | ||
|
|
||
| throw new Error("Error copying database"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| / | ||
| Check if the database already exist to avoid re-copying the file each | ||
| time you open the application. | ||
|
|
||
| @return true if it exists, false if it doesn't | ||
| / | ||
| private boolean checkDataBase() { | ||
|
|
||
| SQLiteDatabase checkDB = null; | ||
|
|
||
| try { | ||
| String myPath = DB_PATH + DB_NAME; | ||
| checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | ||
|
|
||
| } catch (SQLiteException e) { | ||
| // database does't exist yet. | ||
| } | ||
| if (checkDB != null) { | ||
| checkDB.close(); | ||
| } | ||
| return checkDB != null ? true : false; | ||
| } | ||
|
|
||
| / | ||
| Copies your database from your local assets-folder to the just created | ||
| empty database in the system folder, from where it can be accessed and | ||
| handled. This is done by transfering bytestream. | ||
| / | ||
| private void copyDataBase() throws IOException { | ||
|
|
||
| // Open your local db as the input stream | ||
| InputStream myInput = myContext.getAssets().open(DB_NAME); | ||
|
|
||
| // Path to the just created empty db | ||
| String outFileName = DB_PATH + DB_NAME; | ||
|
|
||
| // Open the empty db as the output stream | ||
| OutputStream myOutput = new FileOutputStream(outFileName); | ||
|
|
||
| // transfer bytes from the inputfile to the outputfile | ||
| byte[] buffer = new byte[1024]; | ||
| int length; | ||
| while ((length = myInput.read(buffer)) > 0) { | ||
| myOutput.write(buffer, 0, length); | ||
| } | ||
|
|
||
| // Close the streams | ||
| myOutput.flush(); | ||
| myOutput.close(); | ||
| myInput.close(); | ||
|
|
||
| } | ||
|
|
||
| public void openDataBase() throws SQLException { | ||
|
|
||
| // Open the database | ||
| String myPath = DB_PATH + DB_NAME; | ||
| myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public synchronized void close() { | ||
|
|
||
| if (myDataBase != null) | ||
| myDataBase.close(); | ||
|
|
||
| super.close(); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onCreate(SQLiteDatabase db) { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | ||
|
|
||
| } | ||
|
|
||
| public List<String> getAllLabels() { | ||
| List<String> labels = new ArrayList<String>(); | ||
|
|
||
| // Select All Query | ||
| String selectQuery = "SELECT FROM " + tableName; | ||
|
|
||
| SQLiteDatabase db = this.getReadableDatabase(); | ||
| Cursor cursor = db.rawQuery(selectQuery, null); | ||
|
|
||
| // looping through all rows and adding to list | ||
| if (cursor.moveToFirst()) { | ||
| do { | ||
| labels.add(cursor.getString(1) + " " + cursor.getString(2) + ", " + cursor.getString(3)); | ||
| } while (cursor.moveToNext()); | ||
| } | ||
|
|
||
| // closing connection | ||
| cursor.close(); | ||
| db.close(); | ||
|
|
||
| // returning labels | ||
| return labels; | ||
|
|
||
| } // will returns all labels stored in database | ||
| public List<String> getBeerDrinkLabels(){ | ||
|
|
||
| List<String> allBeerDrinkLabels = new ArrayList<String>(); | ||
|
|
||
| String sqlquery="SELECT FROM " + barTableName + " WHERE DRINKTYPE='Beer';"; | ||
| String selectQuery = sqlquery; | ||
|
|
||
| SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = | ||
| db.rawQuery(selectQuery, null); | ||
|
|
||
| // looping through all rows and adding to list if (cursor.moveToFirst()) | ||
| { do { allBeerDrinkLabels.add(cursor.getString(1) + " Price: " + | ||
| cursor.getString(2)); } while (cursor.moveToNext()); } | ||
|
|
||
| // closing connection cursor.close(); db.close(); | ||
|
|
||
| // returning labels return allBeerDrinkLabels; | ||
|
|
||
| } // will returns all labels stored in database | ||
|
|
||
| } |
| @@ -0,0 +1,193 @@ | ||
| package com.example.sixth; | ||
|
|
||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import android.content.Context; | ||
| import android.database.Cursor; | ||
| import android.database.SQLException; | ||
| import android.database.sqlite.SQLiteDatabase; | ||
| import android.database.sqlite.SQLiteException; | ||
| import android.database.sqlite.SQLiteOpenHelper; | ||
| import android.os.Bundle; | ||
|
|
||
| public class DBHelper extends SQLiteOpenHelper { | ||
|
|
||
| private static String DB_PATH = "/data/data/com.example.sixth/databases/"; | ||
| private static String DB_NAME = "BarSample.db"; | ||
| private final Context myContext; | ||
| public static String tableName = "BARS"; | ||
| public String barTableName = Bar.barTableName; | ||
| String sqlquery; | ||
|
|
||
| private SQLiteDatabase myDataBase; | ||
|
|
||
| public DBHelper(Context context) { | ||
|
|
||
| super(context, DB_NAME, null, 1); | ||
| this.myContext = context; | ||
| } | ||
|
|
||
| // Creates a empty database on the system and rewrites it with your own | ||
| // database. | ||
|
|
||
| public void createDataBase() throws IOException { | ||
|
|
||
| boolean dbExist = checkDataBase(); | ||
|
|
||
| if (dbExist) { | ||
| // do nothing - database already exist | ||
| } else { | ||
|
|
||
| // By calling this method and empty database will be created into | ||
| // the default system path | ||
| // of your application so we are gonna be able to overwrite that | ||
| // database with our database. | ||
| this.getReadableDatabase(); | ||
|
|
||
| try { | ||
| this.close(); | ||
| copyDataBase(); | ||
|
|
||
| } catch (IOException e) { | ||
|
|
||
| throw new Error("Error copying database"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Check if the database already exist to avoid re-copying the file each | ||
| // time you open the application. | ||
|
|
||
| // @return true if it exists, false if it doesn't | ||
|
|
||
| private boolean checkDataBase() { | ||
|
|
||
| SQLiteDatabase checkDB = null; | ||
|
|
||
| try { | ||
| String myPath = DB_PATH + DB_NAME; | ||
| checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | ||
|
|
||
| } catch (SQLiteException e) { | ||
| // database does't exist yet. | ||
| } | ||
| if (checkDB != null) { | ||
| checkDB.close(); | ||
| } | ||
| return checkDB != null ? true : false; | ||
| } | ||
|
|
||
| // Copies your database from your local assets-folder to the just created | ||
| // empty database in the system folder, from where it can be accessed and | ||
| // handled. This is done by transfering bytestream. | ||
|
|
||
| private void copyDataBase() throws IOException { | ||
|
|
||
| // Open your local db as the input stream | ||
| InputStream myInput = myContext.getAssets().open(DB_NAME); | ||
|
|
||
| // Path to the just created empty db | ||
| String outFileName = DB_PATH + DB_NAME; | ||
|
|
||
| // Open the empty db as the output stream | ||
| OutputStream myOutput = new FileOutputStream(outFileName); | ||
|
|
||
| // transfer bytes from the inputfile to the outputfile | ||
| byte[] buffer = new byte[1024]; | ||
| int length; | ||
| while ((length = myInput.read(buffer)) > 0) { | ||
| myOutput.write(buffer, 0, length); | ||
| } | ||
|
|
||
| // Close the streams | ||
| myOutput.flush(); | ||
| myOutput.close(); | ||
| myInput.close(); | ||
|
|
||
| } | ||
|
|
||
| public void openDataBase() throws SQLException { | ||
|
|
||
| // Open the database | ||
| String myPath = DB_PATH + DB_NAME; | ||
| myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public synchronized void close() { | ||
|
|
||
| if (myDataBase != null) | ||
| myDataBase.close(); | ||
|
|
||
| super.close(); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onCreate(SQLiteDatabase db) { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | ||
|
|
||
| } | ||
|
|
||
| public List<String> getAllLabels() { | ||
| List<String> labels = new ArrayList<String>(); | ||
|
|
||
| // Select All Query | ||
| String selectQuery = "SELECT * FROM " + tableName; | ||
|
|
||
| SQLiteDatabase db = this.getReadableDatabase(); | ||
| Cursor cursor = db.rawQuery(selectQuery, null); | ||
|
|
||
| // looping through all rows and adding to list | ||
| if (cursor.moveToFirst()) { | ||
| do { | ||
| labels.add(cursor.getString(1) + " " + cursor.getString(2) + ", " + cursor.getString(3)); | ||
| } while (cursor.moveToNext()); | ||
| } | ||
|
|
||
| // closing connection | ||
| cursor.close(); | ||
| db.close(); | ||
|
|
||
| // returning labels | ||
| return labels; | ||
|
|
||
| } // will returns all labels stored in database | ||
|
|
||
| public List<String> getBeerDrinkLabels() { | ||
|
|
||
| List<String> allBeerDrinkLabels = new ArrayList<String>(); | ||
|
|
||
| String sqlquery = "SELECT * FROM " + barTableName + ";"; | ||
| String selectQuery = sqlquery; | ||
|
|
||
| SQLiteDatabase db = this.getReadableDatabase(); | ||
| Cursor cursorBeer = db.rawQuery(selectQuery, null); | ||
|
|
||
| // looping through all rows and adding to list if (cursor.moveToFirst()) | ||
| { | ||
| do { | ||
| allBeerDrinkLabels.add(cursorBeer.getString(1)); | ||
| } while (cursorBeer.moveToNext()); | ||
| } | ||
|
|
||
| // closing connection | ||
| cursorBeer.close(); | ||
| db.close(); | ||
|
|
||
| // returning labels | ||
| return allBeerDrinkLabels; | ||
|
|
||
| } // will returns all labels stored in database | ||
|
|
||
| } |
| @@ -0,0 +1,171 @@ | ||
| package com.example.sixth; | ||
|
|
||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import android.content.Context; | ||
| import android.database.Cursor; | ||
| import android.database.SQLException; | ||
| import android.database.sqlite.SQLiteDatabase; | ||
| import android.database.sqlite.SQLiteException; | ||
| import android.database.sqlite.SQLiteOpenHelper; | ||
|
|
||
| public class DBHelper extends SQLiteOpenHelper { | ||
|
|
||
| private static String DB_PATH = "/data/data/com.example.sixth/databases/"; | ||
| private static String DB_NAME = "BarSample.db"; | ||
| private final Context myContext; | ||
| public static String tableName = "BARS"; | ||
| String barTableName, sqlquery; | ||
| public String firstBarTableName = MainActivity.upperCaseName;// Pass in the | ||
| // specific | ||
| // bar from | ||
| // the | ||
| // spinner | ||
| // choice | ||
| private SQLiteDatabase myDataBase; | ||
|
|
||
| public DBHelper(Context context) { | ||
|
|
||
| super(context, DB_NAME, null, 1); | ||
| this.myContext = context; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a empty database on the system and rewrites it with your own | ||
| * database. | ||
| */ | ||
| public void createDataBase() throws IOException { | ||
|
|
||
| boolean dbExist = checkDataBase(); | ||
|
|
||
| if (dbExist) { | ||
| // do nothing - database already exist | ||
| } else { | ||
|
|
||
| // By calling this method and empty database will be created into | ||
| // the default system path | ||
| // of your application so we are gonna be able to overwrite that | ||
| // database with our database. | ||
| this.getReadableDatabase(); | ||
|
|
||
| try { | ||
| this.close(); | ||
| copyDataBase(); | ||
|
|
||
| } catch (IOException e) { | ||
|
|
||
| throw new Error("Error copying database"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Check if the database already exist to avoid re-copying the file each | ||
| * time you open the application. | ||
| * | ||
| * @return true if it exists, false if it doesn't | ||
| */ | ||
| private boolean checkDataBase() { | ||
|
|
||
| SQLiteDatabase checkDB = null; | ||
|
|
||
| try { | ||
| String myPath = DB_PATH + DB_NAME; | ||
| checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | ||
|
|
||
| } catch (SQLiteException e) { | ||
| // database does't exist yet. | ||
| } | ||
| if (checkDB != null) { | ||
| checkDB.close(); | ||
| } | ||
| return checkDB != null ? true : false; | ||
| } | ||
|
|
||
| /** | ||
| * Copies your database from your local assets-folder to the just created | ||
| * empty database in the system folder, from where it can be accessed and | ||
| * handled. This is done by transfering bytestream. | ||
| */ | ||
| private void copyDataBase() throws IOException { | ||
|
|
||
| // Open your local db as the input stream | ||
| InputStream myInput = myContext.getAssets().open(DB_NAME); | ||
|
|
||
| // Path to the just created empty db | ||
| String outFileName = DB_PATH + DB_NAME; | ||
|
|
||
| // Open the empty db as the output stream | ||
| OutputStream myOutput = new FileOutputStream(outFileName); | ||
|
|
||
| // transfer bytes from the inputfile to the outputfile | ||
| byte[] buffer = new byte[1024]; | ||
| int length; | ||
| while ((length = myInput.read(buffer)) > 0) { | ||
| myOutput.write(buffer, 0, length); | ||
| } | ||
|
|
||
| // Close the streams | ||
| myOutput.flush(); | ||
| myOutput.close(); | ||
| myInput.close(); | ||
|
|
||
| } | ||
|
|
||
| public void openDataBase() throws SQLException { | ||
|
|
||
| // Open the database | ||
| String myPath = DB_PATH + DB_NAME; | ||
| myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public synchronized void close() { | ||
|
|
||
| if (myDataBase != null) | ||
| myDataBase.close(); | ||
|
|
||
| super.close(); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onCreate(SQLiteDatabase db) { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | ||
|
|
||
| } | ||
|
|
||
| public List<String> getAllLabels() { | ||
| List<String> labels = new ArrayList<String>(); | ||
|
|
||
| // Select All Query | ||
| String selectQuery = "SELECT * FROM " + tableName; | ||
|
|
||
| SQLiteDatabase db = this.getReadableDatabase(); | ||
| Cursor cursor = db.rawQuery(selectQuery, null); | ||
|
|
||
| // looping through all rows and adding to list | ||
| if (cursor.moveToFirst()) { | ||
| do { | ||
| labels.add(cursor.getString(1) + " " + cursor.getString(2) + ", " + cursor.getString(3)); | ||
| } while (cursor.moveToNext()); | ||
| } | ||
|
|
||
| // closing connection | ||
| cursor.close(); | ||
| db.close(); | ||
|
|
||
| // returning labels | ||
| return labels; | ||
|
|
||
| } // will returns all labels stored in database |
| @@ -0,0 +1,98 @@ | ||
| package com.example.sixth; | ||
|
|
||
| import android.app.Activity; | ||
| import android.content.Intent; | ||
| import android.os.Bundle; | ||
| import android.view.View; | ||
| import android.view.View.OnClickListener; | ||
| import android.widget.Button; | ||
| import android.widget.ImageView; | ||
| import android.widget.TextView; | ||
|
|
||
| public class Bar extends Activity{ | ||
| String setBarTest; | ||
| String barNameHolder, picHolder, barContactHolder, barPhoneHolder; | ||
| static String barTableName; | ||
| int imageInt, textInt1,textInt2, textInt3, textInt4; | ||
| TextView setBarName, setBarContact,setBarPhone, setBarHours; | ||
| TextView setBarTester; | ||
| ImageView barPic; | ||
| Button viewAll, beer, wine, mixedDrinks, other, specials, getTaxi; | ||
|
|
||
|
|
||
| public void onCreate(Bundle savedInstanceState) { | ||
| Bundle bundle = getIntent().getExtras(); | ||
| if (bundle != null) { | ||
| setBarTest = bundle.getString("bar"); | ||
| //barName =barNameHolder; | ||
| } | ||
|
|
||
| barTableName=setBarTest.toUpperCase(); | ||
|
|
||
| super.onCreate(savedInstanceState); | ||
| setContentView(R.layout.activity_bar); | ||
|
|
||
| Button viewAll = (Button)findViewById(R.id.btnviewAll); | ||
| Button specials = (Button)findViewById(R.id.btnSpecials); | ||
| Button getTaxi = (Button)findViewById(R.id.btnTaxi); | ||
|
|
||
| barPic = (ImageView) findViewById(R.id.barPic); | ||
| String picHolder = "drawable/"+setBarTest; | ||
| imageInt = getResources().getIdentifier(picHolder, null, getPackageName()); | ||
| barPic.setImageResource(imageInt); | ||
|
|
||
| setBarName = (TextView)findViewById(R.id.barName); | ||
| String barNameHolder = "@string/"+setBarTest; | ||
| textInt1 = getResources().getIdentifier(barNameHolder, null, getPackageName()); | ||
| setBarName.setText(textInt1); | ||
|
|
||
| setBarContact = (TextView)findViewById(R.id.barContact); | ||
| String barContactHolder = "@string/"+setBarTest+"Contact"; | ||
| textInt2 = getResources().getIdentifier(barContactHolder, null, getPackageName()); | ||
| setBarContact.setText(textInt2); | ||
|
|
||
| setBarPhone = (TextView)findViewById(R.id.barPhone); | ||
| String barPhoneHolder = "@string/"+setBarTest+"Phone"; | ||
| textInt3 = getResources().getIdentifier(barPhoneHolder, null, getPackageName()); | ||
| setBarPhone.setText(textInt3); | ||
|
|
||
| setBarHours = (TextView)findViewById(R.id.barHours); | ||
| String barHoursHolder = "@string/"+setBarTest+"Hours"; | ||
| textInt4 = getResources().getIdentifier(barHoursHolder, null, getPackageName()); | ||
| setBarHours.setText(textInt4); | ||
|
|
||
| setBarTester = (TextView)findViewById(R.id.setBarTester); | ||
| String barTesterHolder = setBarTest ; | ||
| //int textInt5 = getResources().getIdentifier(barTesterHolder, null, getPackageName()); | ||
| setBarTester.setText(barTesterHolder); | ||
|
|
||
| viewAll.setOnClickListener(new OnClickListener() { | ||
| @Override | ||
| public void onClick(View v) { | ||
| Intent i = (new Intent(Bar.this, Drinks.class)); | ||
| startActivity(i); | ||
| } | ||
|
|
||
| }); | ||
|
|
||
| specials.setOnClickListener(new OnClickListener() { | ||
| @Override | ||
| public void onClick(View v) { | ||
| Intent i = (new Intent(Bar.this, NoResult.class)); | ||
| startActivity(i); | ||
| } | ||
|
|
||
| }); | ||
|
|
||
| getTaxi.setOnClickListener(new OnClickListener() { | ||
| @Override | ||
| public void onClick(View v) { | ||
| Intent i = (new Intent(Bar.this, Taxi.class)); | ||
| startActivity(i); | ||
| } | ||
|
|
||
| }); | ||
|
|
||
|
|
||
| } | ||
| } |
| @@ -0,0 +1,199 @@ | ||
| package com.example.sixth; | ||
|
|
||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import android.content.Context; | ||
| import android.database.Cursor; | ||
| import android.database.SQLException; | ||
| import android.database.sqlite.SQLiteDatabase; | ||
| import android.database.sqlite.SQLiteException; | ||
| import android.database.sqlite.SQLiteOpenHelper; | ||
| import android.os.Bundle; | ||
|
|
||
| public class DBHelper extends SQLiteOpenHelper { | ||
|
|
||
| private static String DB_PATH = "/data/data/com.example.sixth/databases/"; | ||
| private static String DB_NAME = "BarSample.db"; | ||
| private final Context myContext; | ||
| public static String tableName = "BARS"; | ||
| public String barTableName = Bar.barTableName; | ||
|
|
||
| String sqlquery; | ||
| // String firstBarTableName = MainActivity.upperCaseName;// Pass in the | ||
| // specific | ||
| // bar from | ||
| // the | ||
| // spinner | ||
| // choice | ||
| private SQLiteDatabase myDataBase; | ||
|
|
||
| public DBHelper(Context context) { | ||
|
|
||
| super(context, DB_NAME, null, 1); | ||
| this.myContext = context; | ||
| } | ||
|
|
||
| // Creates a empty database on the system and rewrites it with your own | ||
| // database. | ||
|
|
||
| public void createDataBase() throws IOException { | ||
|
|
||
| boolean dbExist = checkDataBase(); | ||
|
|
||
| if (dbExist) { | ||
| // do nothing - database already exist | ||
| } else { | ||
|
|
||
| // By calling this method and empty database will be created into | ||
| // the default system path | ||
| // of your application so we are gonna be able to overwrite that | ||
| // database with our database. | ||
| this.getReadableDatabase(); | ||
|
|
||
| try { | ||
| this.close(); | ||
| copyDataBase(); | ||
|
|
||
| } catch (IOException e) { | ||
|
|
||
| throw new Error("Error copying database"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Check if the database already exist to avoid re-copying the file each | ||
| // time you open the application. | ||
|
|
||
| // @return true if it exists, false if it doesn't | ||
|
|
||
| private boolean checkDataBase() { | ||
|
|
||
| SQLiteDatabase checkDB = null; | ||
|
|
||
| try { | ||
| String myPath = DB_PATH + DB_NAME; | ||
| checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | ||
|
|
||
| } catch (SQLiteException e) { | ||
| // database does't exist yet. | ||
| } | ||
| if (checkDB != null) { | ||
| checkDB.close(); | ||
| } | ||
| return checkDB != null ? true : false; | ||
| } | ||
|
|
||
| // Copies your database from your local assets-folder to the just created | ||
| // empty database in the system folder, from where it can be accessed and | ||
| // handled. This is done by transfering bytestream. | ||
|
|
||
| private void copyDataBase() throws IOException { | ||
|
|
||
| // Open your local db as the input stream | ||
| InputStream myInput = myContext.getAssets().open(DB_NAME); | ||
|
|
||
| // Path to the just created empty db | ||
| String outFileName = DB_PATH + DB_NAME; | ||
|
|
||
| // Open the empty db as the output stream | ||
| OutputStream myOutput = new FileOutputStream(outFileName); | ||
|
|
||
| // transfer bytes from the inputfile to the outputfile | ||
| byte[] buffer = new byte[1024]; | ||
| int length; | ||
| while ((length = myInput.read(buffer)) > 0) { | ||
| myOutput.write(buffer, 0, length); | ||
| } | ||
|
|
||
| // Close the streams | ||
| myOutput.flush(); | ||
| myOutput.close(); | ||
| myInput.close(); | ||
|
|
||
| } | ||
|
|
||
| public void openDataBase() throws SQLException { | ||
|
|
||
| // Open the database | ||
| String myPath = DB_PATH + DB_NAME; | ||
| myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public synchronized void close() { | ||
|
|
||
| if (myDataBase != null) | ||
| myDataBase.close(); | ||
|
|
||
| super.close(); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onCreate(SQLiteDatabase db) { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | ||
|
|
||
| } | ||
|
|
||
| public List<String> getAllLabels() { | ||
| List<String> labels = new ArrayList<String>(); | ||
|
|
||
| // Select All Query | ||
| String selectQuery = "SELECT * FROM " + tableName; | ||
|
|
||
| SQLiteDatabase db = this.getReadableDatabase(); | ||
| Cursor cursor = db.rawQuery(selectQuery, null); | ||
|
|
||
| // looping through all rows and adding to list | ||
| if (cursor.moveToFirst()) { | ||
| do { | ||
| labels.add(cursor.getString(1) + " " + cursor.getString(2) + ", " + cursor.getString(3)); | ||
| } while (cursor.moveToNext()); | ||
| } | ||
|
|
||
| // closing connection | ||
| cursor.close(); | ||
| db.close(); | ||
|
|
||
| // returning labels | ||
| return labels; | ||
|
|
||
| } // will returns all labels stored in database | ||
|
|
||
| public List<String> getBeerDrinkLabels() { | ||
|
|
||
| List<String> allBeerDrinkLabels = new ArrayList<String>(); | ||
|
|
||
| String sqlquery = "SELECT * FROM " + barTableName + " WHERE DRINKTYPE='Beer';"; | ||
| String selectQuery = sqlquery; | ||
|
|
||
| SQLiteDatabase db = this.getReadableDatabase(); | ||
| Cursor cursor = db.rawQuery(selectQuery, null); | ||
|
|
||
| // looping through all rows and adding to list if (cursor.moveToFirst()) | ||
| { | ||
| do { | ||
| allBeerDrinkLabels.add(cursor.getString(0) + " Price: " + cursor.getString(1)); | ||
| } while (cursor.moveToNext()); | ||
| } | ||
|
|
||
| // closing connection | ||
| cursor.close(); | ||
| db.close(); | ||
|
|
||
| // returning labels | ||
| return allBeerDrinkLabels; | ||
|
|
||
| } // will returns all labels stored in database | ||
|
|
||
| } |
| @@ -0,0 +1,192 @@ | ||
| package com.example.sixth; | ||
|
|
||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import android.content.Context; | ||
| import android.database.Cursor; | ||
| import android.database.SQLException; | ||
| import android.database.sqlite.SQLiteDatabase; | ||
| import android.database.sqlite.SQLiteException; | ||
| import android.database.sqlite.SQLiteOpenHelper; | ||
|
|
||
| public class DBHelper extends SQLiteOpenHelper { | ||
|
|
||
| private static String DB_PATH = "/data/data/com.example.sixth/databases/"; | ||
| private static String DB_NAME = "BarSample.db"; | ||
| private final Context myContext; | ||
| public static String tableName = "BARS"; | ||
| String barTableName, sqlquery; | ||
| //String firstBarTableName = MainActivity.upperCaseName;// Pass in the | ||
| // specific | ||
| // bar from | ||
| // the | ||
| // spinner | ||
| // choice | ||
| private SQLiteDatabase myDataBase; | ||
|
|
||
| public DBHelper(Context context) { | ||
|
|
||
| super(context, DB_NAME, null, 1); | ||
| this.myContext = context; | ||
| } | ||
|
|
||
| / | ||
| Creates a empty database on the system and rewrites it with your own | ||
| database. | ||
| / | ||
| public void createDataBase() throws IOException { | ||
|
|
||
| boolean dbExist = checkDataBase(); | ||
|
|
||
| if (dbExist) { | ||
| // do nothing - database already exist | ||
| } else { | ||
|
|
||
| // By calling this method and empty database will be created into | ||
| // the default system path | ||
| // of your application so we are gonna be able to overwrite that | ||
| // database with our database. | ||
| this.getReadableDatabase(); | ||
|
|
||
| try { | ||
| this.close(); | ||
| copyDataBase(); | ||
|
|
||
| } catch (IOException e) { | ||
|
|
||
| throw new Error("Error copying database"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| / | ||
| Check if the database already exist to avoid re-copying the file each | ||
| time you open the application. | ||
|
|
||
| @return true if it exists, false if it doesn't | ||
| / | ||
| private boolean checkDataBase() { | ||
|
|
||
| SQLiteDatabase checkDB = null; | ||
|
|
||
| try { | ||
| String myPath = DB_PATH + DB_NAME; | ||
| checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | ||
|
|
||
| } catch (SQLiteException e) { | ||
| // database does't exist yet. | ||
| } | ||
| if (checkDB != null) { | ||
| checkDB.close(); | ||
| } | ||
| return checkDB != null ? true : false; | ||
| } | ||
|
|
||
| / | ||
| Copies your database from your local assets-folder to the just created | ||
| empty database in the system folder, from where it can be accessed and | ||
| handled. This is done by transfering bytestream. | ||
| / | ||
| private void copyDataBase() throws IOException { | ||
|
|
||
| // Open your local db as the input stream | ||
| InputStream myInput = myContext.getAssets().open(DB_NAME); | ||
|
|
||
| // Path to the just created empty db | ||
| String outFileName = DB_PATH + DB_NAME; | ||
|
|
||
| // Open the empty db as the output stream | ||
| OutputStream myOutput = new FileOutputStream(outFileName); | ||
|
|
||
| // transfer bytes from the inputfile to the outputfile | ||
| byte[] buffer = new byte[1024]; | ||
| int length; | ||
| while ((length = myInput.read(buffer)) > 0) { | ||
| myOutput.write(buffer, 0, length); | ||
| } | ||
|
|
||
| // Close the streams | ||
| myOutput.flush(); | ||
| myOutput.close(); | ||
| myInput.close(); | ||
|
|
||
| } | ||
|
|
||
| public void openDataBase() throws SQLException { | ||
|
|
||
| // Open the database | ||
| String myPath = DB_PATH + DB_NAME; | ||
| myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public synchronized void close() { | ||
|
|
||
| if (myDataBase != null) | ||
| myDataBase.close(); | ||
|
|
||
| super.close(); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onCreate(SQLiteDatabase db) { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | ||
|
|
||
| } | ||
|
|
||
| public List<String> getAllLabels() { | ||
| List<String> labels = new ArrayList<String>(); | ||
|
|
||
| // Select All Query | ||
| String selectQuery = "SELECT FROM " + tableName; | ||
|
|
||
| SQLiteDatabase db = this.getReadableDatabase(); | ||
| Cursor cursor = db.rawQuery(selectQuery, null); | ||
|
|
||
| // looping through all rows and adding to list | ||
| if (cursor.moveToFirst()) { | ||
| do { | ||
| labels.add(cursor.getString(1) + " " + cursor.getString(2) + ", " + cursor.getString(3)); | ||
| } while (cursor.moveToNext()); | ||
| } | ||
|
|
||
| // closing connection | ||
| cursor.close(); | ||
| db.close(); | ||
|
|
||
| // returning labels | ||
| return labels; | ||
|
|
||
| } // will returns all labels stored in database | ||
| public List<String> getBeerDrinkLabels(){ | ||
|
|
||
| List<String> allBeerDrinkLabels = new ArrayList<String>(); | ||
|
|
||
| String sqlquery="SELECT FROM " + barTableName + " WHERE DRINKTYPE='Beer';"; | ||
| String selectQuery = sqlquery; | ||
|
|
||
| SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = | ||
| db.rawQuery(selectQuery, null); | ||
|
|
||
| // looping through all rows and adding to list if (cursor.moveToFirst()) | ||
| { do { allBeerDrinkLabels.add(cursor.getString(1) + " Price: " + | ||
| cursor.getString(2)); } while (cursor.moveToNext()); } | ||
|
|
||
| // closing connection cursor.close(); db.close(); | ||
|
|
||
| // returning labels return allBeerDrinkLabels; | ||
|
|
||
| } // will returns all labels stored in database | ||
|
|
||
|
|
| @@ -0,0 +1,194 @@ | ||
| package com.example.sixth; | ||
|
|
||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import android.content.Context; | ||
| import android.database.Cursor; | ||
| import android.database.SQLException; | ||
| import android.database.sqlite.SQLiteDatabase; | ||
| import android.database.sqlite.SQLiteException; | ||
| import android.database.sqlite.SQLiteOpenHelper; | ||
|
|
||
| public class DBHelper extends SQLiteOpenHelper { | ||
|
|
||
| private static String DB_PATH = "/data/data/com.example.sixth/databases/"; | ||
| private static String DB_NAME = "BarSample.db"; | ||
| private final Context myContext; | ||
| public static String tableName = "BARS"; | ||
| String barTableName, sqlquery; | ||
| //String firstBarTableName = MainActivity.upperCaseName;// Pass in the | ||
| // specific | ||
| // bar from | ||
| // the | ||
| // spinner | ||
| // choice | ||
| private SQLiteDatabase myDataBase; | ||
|
|
||
| public DBHelper(Context context) { | ||
|
|
||
| super(context, DB_NAME, null, 1); | ||
| this.myContext = context; | ||
| } | ||
|
|
||
| / | ||
| Creates a empty database on the system and rewrites it with your own | ||
| database. | ||
| / | ||
| public void createDataBase() throws IOException { | ||
|
|
||
| boolean dbExist = checkDataBase(); | ||
|
|
||
| if (dbExist) { | ||
| // do nothing - database already exist | ||
| } else { | ||
|
|
||
| // By calling this method and empty database will be created into | ||
| // the default system path | ||
| // of your application so we are gonna be able to overwrite that | ||
| // database with our database. | ||
| this.getReadableDatabase(); | ||
|
|
||
| try { | ||
| this.close(); | ||
| copyDataBase(); | ||
|
|
||
| } catch (IOException e) { | ||
|
|
||
| throw new Error("Error copying database"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| / | ||
| Check if the database already exist to avoid re-copying the file each | ||
| time you open the application. | ||
|
|
||
| @return true if it exists, false if it doesn't | ||
| / | ||
| private boolean checkDataBase() { | ||
|
|
||
| SQLiteDatabase checkDB = null; | ||
|
|
||
| try { | ||
| String myPath = DB_PATH + DB_NAME; | ||
| checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | ||
|
|
||
| } catch (SQLiteException e) { | ||
| // database does't exist yet. | ||
| } | ||
| if (checkDB != null) { | ||
| checkDB.close(); | ||
| } | ||
| return checkDB != null ? true : false; | ||
| } | ||
|
|
||
| / | ||
| Copies your database from your local assets-folder to the just created | ||
| empty database in the system folder, from where it can be accessed and | ||
| handled. This is done by transfering bytestream. | ||
| / | ||
| private void copyDataBase() throws IOException { | ||
|
|
||
| // Open your local db as the input stream | ||
| InputStream myInput = myContext.getAssets().open(DB_NAME); | ||
|
|
||
| // Path to the just created empty db | ||
| String outFileName = DB_PATH + DB_NAME; | ||
|
|
||
| // Open the empty db as the output stream | ||
| OutputStream myOutput = new FileOutputStream(outFileName); | ||
|
|
||
| // transfer bytes from the inputfile to the outputfile | ||
| byte[] buffer = new byte[1024]; | ||
| int length; | ||
| while ((length = myInput.read(buffer)) > 0) { | ||
| myOutput.write(buffer, 0, length); | ||
| } | ||
|
|
||
| // Close the streams | ||
| myOutput.flush(); | ||
| myOutput.close(); | ||
| myInput.close(); | ||
|
|
||
| } | ||
|
|
||
| public void openDataBase() throws SQLException { | ||
|
|
||
| // Open the database | ||
| String myPath = DB_PATH + DB_NAME; | ||
| myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public synchronized void close() { | ||
|
|
||
| if (myDataBase != null) | ||
| myDataBase.close(); | ||
|
|
||
| super.close(); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onCreate(SQLiteDatabase db) { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | ||
|
|
||
| } | ||
|
|
||
| public List<String> getAllLabels() { | ||
| List<String> labels = new ArrayList<String>(); | ||
|
|
||
| // Select All Query | ||
| String selectQuery = "SELECT FROM " + tableName; | ||
|
|
||
| SQLiteDatabase db = this.getReadableDatabase(); | ||
| Cursor cursor = db.rawQuery(selectQuery, null); | ||
|
|
||
| // looping through all rows and adding to list | ||
| if (cursor.moveToFirst()) { | ||
| do { | ||
| labels.add(cursor.getString(1) + " " + cursor.getString(2) + ", " + cursor.getString(3)); | ||
| } while (cursor.moveToNext()); | ||
| } | ||
|
|
||
| // closing connection | ||
| cursor.close(); | ||
| db.close(); | ||
|
|
||
| // returning labels | ||
| return labels; | ||
|
|
||
| } // will returns all labels stored in database | ||
| public List<String> getBeerDrinkLabels(){ | ||
|
|
||
| List<String> allBeerDrinkLabels = new ArrayList<String>(); | ||
|
|
||
| String sqlquery="SELECT FROM " + barTableName + " WHERE DRINKTYPE='Beer';"; | ||
| String selectQuery = sqlquery; | ||
|
|
||
| SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = | ||
| db.rawQuery(selectQuery, null); | ||
|
|
||
| // looping through all rows and adding to list if (cursor.moveToFirst()) | ||
| { do { allBeerDrinkLabels.add(cursor.getString(1) + " Price: " + | ||
| cursor.getString(2)); } while (cursor.moveToNext()); } | ||
|
|
||
| // closing connection | ||
| cursor.close(); | ||
| db.close(); | ||
|
|
||
| // returning labels return allBeerDrinkLabels; | ||
|
|
||
| } // will returns all labels stored in database | ||
|
|
||
|
|
| @@ -0,0 +1,95 @@ | ||
| package com.example.sixth; | ||
|
|
||
| import android.app.Activity; | ||
| import android.content.Intent; | ||
| import android.os.Bundle; | ||
| import android.view.View; | ||
| import android.view.View.OnClickListener; | ||
| import android.widget.Button; | ||
| import android.widget.ImageView; | ||
| import android.widget.TextView; | ||
|
|
||
| public class Bar extends Activity{ | ||
| String setBarTest; | ||
| String barNameHolder, picHolder, barContactHolder, barPhoneHolder; | ||
| int imageInt, textInt1,textInt2, textInt3, textInt4; | ||
| TextView setBarName, setBarContact,setBarPhone, setBarHours; | ||
| TextView setBarTester; | ||
| ImageView barPic; | ||
| Button viewAll, beer, wine, mixedDrinks, other, specials, getTaxi; | ||
|
|
||
|
|
||
| public void onCreate(Bundle savedInstanceState) { | ||
| Bundle bundle = getIntent().getExtras(); | ||
| if (bundle != null) { | ||
| setBarTest = bundle.getString("bar"); | ||
| //barName =barNameHolder; | ||
| } | ||
|
|
||
| super.onCreate(savedInstanceState); | ||
| setContentView(R.layout.activity_bar); | ||
|
|
||
| Button viewAll = (Button)findViewById(R.id.btnviewAll); | ||
| Button specials = (Button)findViewById(R.id.btnSpecials); | ||
| Button getTaxi = (Button)findViewById(R.id.btnTaxi); | ||
|
|
||
| barPic = (ImageView) findViewById(R.id.barPic); | ||
| String picHolder = "drawable/"+setBarTest; | ||
| imageInt = getResources().getIdentifier(picHolder, null, getPackageName()); | ||
| barPic.setImageResource(imageInt); | ||
|
|
||
| setBarName = (TextView)findViewById(R.id.barName); | ||
| String barNameHolder = "@string/"+setBarTest; | ||
| textInt1 = getResources().getIdentifier(barNameHolder, null, getPackageName()); | ||
| setBarName.setText(textInt1); | ||
|
|
||
| setBarContact = (TextView)findViewById(R.id.barContact); | ||
| String barContactHolder = "@string/"+setBarTest+"Contact"; | ||
| textInt2 = getResources().getIdentifier(barContactHolder, null, getPackageName()); | ||
| setBarContact.setText(textInt2); | ||
|
|
||
| setBarPhone = (TextView)findViewById(R.id.barPhone); | ||
| String barPhoneHolder = "@string/"+setBarTest+"Phone"; | ||
| textInt3 = getResources().getIdentifier(barPhoneHolder, null, getPackageName()); | ||
| setBarPhone.setText(textInt3); | ||
|
|
||
| setBarHours = (TextView)findViewById(R.id.barHours); | ||
| String barHoursHolder = "@string/"+setBarTest+"Hours"; | ||
| textInt4 = getResources().getIdentifier(barHoursHolder, null, getPackageName()); | ||
| setBarHours.setText(textInt4); | ||
|
|
||
| setBarTester = (TextView)findViewById(R.id.setBarTester); | ||
| String barTesterHolder = setBarTest ; | ||
| //int textInt5 = getResources().getIdentifier(barTesterHolder, null, getPackageName()); | ||
| setBarTester.setText(barTesterHolder); | ||
|
|
||
| viewAll.setOnClickListener(new OnClickListener() { | ||
| @Override | ||
| public void onClick(View v) { | ||
| Intent i = (new Intent(Bar.this, Drinks.class)); | ||
| startActivity(i); | ||
| } | ||
|
|
||
| }); | ||
|
|
||
| specials.setOnClickListener(new OnClickListener() { | ||
| @Override | ||
| public void onClick(View v) { | ||
| Intent i = (new Intent(Bar.this, NoResult.class)); | ||
| startActivity(i); | ||
| } | ||
|
|
||
| }); | ||
|
|
||
| getTaxi.setOnClickListener(new OnClickListener() { | ||
| @Override | ||
| public void onClick(View v) { | ||
| Intent i = (new Intent(Bar.this, Taxi.class)); | ||
| startActivity(i); | ||
| } | ||
|
|
||
| }); | ||
|
|
||
|
|
||
| } | ||
| } |
| @@ -0,0 +1,193 @@ | ||
| package com.example.sixth; | ||
|
|
||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import android.content.Context; | ||
| import android.database.Cursor; | ||
| import android.database.SQLException; | ||
| import android.database.sqlite.SQLiteDatabase; | ||
| import android.database.sqlite.SQLiteException; | ||
| import android.database.sqlite.SQLiteOpenHelper; | ||
| import android.os.Bundle; | ||
|
|
||
| public class DBHelper extends SQLiteOpenHelper { | ||
|
|
||
| private static String DB_PATH = "/data/data/com.example.sixth/databases/"; | ||
| private static String DB_NAME = "BarSample.db"; | ||
| private final Context myContext; | ||
| public static String tableName = "BARS"; | ||
| public String barTableName = Bar.barTableName; | ||
| String sqlquery; | ||
|
|
||
| private SQLiteDatabase myDataBase; | ||
|
|
||
| public DBHelper(Context context) { | ||
|
|
||
| super(context, DB_NAME, null, 1); | ||
| this.myContext = context; | ||
| } | ||
|
|
||
| // Creates a empty database on the system and rewrites it with your own | ||
| // database. | ||
|
|
||
| public void createDataBase() throws IOException { | ||
|
|
||
| boolean dbExist = checkDataBase(); | ||
|
|
||
| if (dbExist) { | ||
| // do nothing - database already exist | ||
| } else { | ||
|
|
||
| // By calling this method and empty database will be created into | ||
| // the default system path | ||
| // of your application so we are gonna be able to overwrite that | ||
| // database with our database. | ||
| this.getReadableDatabase(); | ||
|
|
||
| try { | ||
| this.close(); | ||
| copyDataBase(); | ||
|
|
||
| } catch (IOException e) { | ||
|
|
||
| throw new Error("Error copying database"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Check if the database already exist to avoid re-copying the file each | ||
| // time you open the application. | ||
|
|
||
| // @return true if it exists, false if it doesn't | ||
|
|
||
| private boolean checkDataBase() { | ||
|
|
||
| SQLiteDatabase checkDB = null; | ||
|
|
||
| try { | ||
| String myPath = DB_PATH + DB_NAME; | ||
| checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | ||
|
|
||
| } catch (SQLiteException e) { | ||
| // database does't exist yet. | ||
| } | ||
| if (checkDB != null) { | ||
| checkDB.close(); | ||
| } | ||
| return checkDB != null ? true : false; | ||
| } | ||
|
|
||
| // Copies your database from your local assets-folder to the just created | ||
| // empty database in the system folder, from where it can be accessed and | ||
| // handled. This is done by transfering bytestream. | ||
|
|
||
| private void copyDataBase() throws IOException { | ||
|
|
||
| // Open your local db as the input stream | ||
| InputStream myInput = myContext.getAssets().open(DB_NAME); | ||
|
|
||
| // Path to the just created empty db | ||
| String outFileName = DB_PATH + DB_NAME; | ||
|
|
||
| // Open the empty db as the output stream | ||
| OutputStream myOutput = new FileOutputStream(outFileName); | ||
|
|
||
| // transfer bytes from the inputfile to the outputfile | ||
| byte[] buffer = new byte[1024]; | ||
| int length; | ||
| while ((length = myInput.read(buffer)) > 0) { | ||
| myOutput.write(buffer, 0, length); | ||
| } | ||
|
|
||
| // Close the streams | ||
| myOutput.flush(); | ||
| myOutput.close(); | ||
| myInput.close(); | ||
|
|
||
| } | ||
|
|
||
| public void openDataBase() throws SQLException { | ||
|
|
||
| // Open the database | ||
| String myPath = DB_PATH + DB_NAME; | ||
| myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public synchronized void close() { | ||
|
|
||
| if (myDataBase != null) | ||
| myDataBase.close(); | ||
|
|
||
| super.close(); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onCreate(SQLiteDatabase db) { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | ||
|
|
||
| } | ||
|
|
||
| public List<String> getAllLabels() { | ||
| List<String> labels = new ArrayList<String>(); | ||
|
|
||
| // Select All Query | ||
| String selectQuery = "SELECT * FROM " + tableName; | ||
|
|
||
| SQLiteDatabase db = this.getReadableDatabase(); | ||
| Cursor cursor = db.rawQuery(selectQuery, null); | ||
|
|
||
| // looping through all rows and adding to list | ||
| if (cursor.moveToFirst()) { | ||
| do { | ||
| labels.add(cursor.getString(1) + " " + cursor.getString(2) + ", " + cursor.getString(3)); | ||
| } while (cursor.moveToNext()); | ||
| } | ||
|
|
||
| // closing connection | ||
| cursor.close(); | ||
| db.close(); | ||
|
|
||
| // returning labels | ||
| return labels; | ||
|
|
||
| } // will returns all labels stored in database | ||
|
|
||
| public List<String> getBeerDrinkLabels() { | ||
|
|
||
| List<String> allBeerDrinkLabels = new ArrayList<String>(); | ||
|
|
||
| String sqlquery = "SELECT * FROM " + barTableName "; | ||
| String selectQuery = sqlquery; | ||
|
|
||
| SQLiteDatabase db = this.getReadableDatabase(); | ||
| Cursor cursorBeer = db.rawQuery(selectQuery, null); | ||
|
|
||
| // looping through all rows and adding to list if (cursor.moveToFirst()) | ||
| { | ||
| do { | ||
| allBeerDrinkLabels.add(cursorBeer.getString(1)); | ||
| } while (cursorBeer.moveToNext()); | ||
| } | ||
|
|
||
| // closing connection | ||
| cursorBeer.close(); | ||
| db.close(); | ||
|
|
||
| // returning labels | ||
| return allBeerDrinkLabels; | ||
|
|
||
| } // will returns all labels stored in database | ||
|
|
||
| } |
| @@ -0,0 +1,133 @@ | ||
| package com.example.sixth; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import android.app.Activity; | ||
| import android.content.Intent; | ||
| import android.database.SQLException; | ||
| import android.os.Bundle; | ||
| import android.view.Menu; | ||
| import android.view.MenuItem; | ||
| import android.view.View; | ||
| import android.view.View.OnClickListener; | ||
| import android.widget.AdapterView; | ||
| import android.widget.AdapterView.OnItemSelectedListener; | ||
| import android.widget.ArrayAdapter; | ||
| import android.widget.Button; | ||
| import android.widget.Spinner; | ||
|
|
||
|
|
||
| public class MainActivity extends Activity implements | ||
| OnItemSelectedListener { | ||
| DBHelper myDB; | ||
| Button selectBar; | ||
| Spinner spinner; | ||
| static String pullBar,setBar,name, cityState, upperCaseName; | ||
|
|
||
| static List<String> beer, beerLables; | ||
|
|
||
| @Override | ||
| public void onCreate(Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
| setContentView(R.layout.activity_main); | ||
| Button selectBar = (Button)findViewById(R.id.btnSelectBar); | ||
| myDB = new DBHelper(this); | ||
| // Spinner element | ||
| spinner = (Spinner) findViewById(R.id.spinner); | ||
| // Spinner click listener | ||
| spinner.setOnItemSelectedListener(this); | ||
|
|
||
| try { | ||
| myDB.createDataBase(); | ||
| } catch (IOException ioe) { | ||
| throw new Error("Unable to create database"); | ||
| } | ||
| try { | ||
| myDB.openDataBase(); | ||
| } catch (SQLException sqle) { | ||
| throw sqle; | ||
| } | ||
|
|
||
|
|
||
| // Loading spinner data from database | ||
| loadSpinnerData(); | ||
|
|
||
| selectBar.setOnClickListener(new OnClickListener() { | ||
| @Override | ||
| public void onClick(View v) { | ||
| pullBar = String.valueOf(spinner.getSelectedItem()); | ||
| String[] parts = pullBar.split(" "); //returns an array with the 2 parts | ||
| name = parts[0]; | ||
|
|
||
| String nlast = parts[parts.length-2]; | ||
| String last = parts[parts.length-1]; | ||
|
|
||
| cityState = nlast+ " " + last; | ||
| upperCaseName=name.toUpperCase(); | ||
| setBar = name.toLowerCase(); | ||
|
|
||
| beer = new ArrayList<String>(); | ||
| beerLables = new ArrayList<String>(); | ||
|
|
||
| Intent i = (new Intent(MainActivity.this, Bar.class)); | ||
| i.putExtra("bar",upperCaseName); | ||
| startActivity(i); | ||
| } | ||
|
|
||
| }); | ||
| } | ||
|
|
||
|
|
||
| private void loadSpinnerData() { | ||
| // database handler | ||
| DBHelper db = new DBHelper(getApplicationContext()); | ||
|
|
||
| // Spinner Drop down elements | ||
| List<String> lables = db.getAllLabels(); | ||
|
|
||
| // Creating adapter for spinner | ||
| ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, | ||
| android.R.layout.simple_spinner_item, lables); | ||
|
|
||
| // Drop down layout style - list view with radio button | ||
| dataAdapter | ||
| .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | ||
|
|
||
| // attaching data adapter to spinner | ||
| spinner.setAdapter(dataAdapter); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean onCreateOptionsMenu(Menu menu) { | ||
| // Inflate the menu; this adds items to the action bar if it is present. | ||
| getMenuInflater().inflate(R.menu.main, menu); | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean onOptionsItemSelected(MenuItem item) { | ||
| // Handle action bar item clicks here. The action bar will | ||
| // automatically handle clicks on the Home/Up button, so long | ||
| // as you specify a parent activity in AndroidManifest.xml. | ||
| int id = item.getItemId(); | ||
| if (id == R.id.action_settings) { | ||
| return true; | ||
| } | ||
| return super.onOptionsItemSelected(item); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { | ||
| // TODO Auto-generated method stub | ||
|
|
||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public void onNothingSelected(AdapterView<?> parent) { | ||
| // TODO Auto-generated method stub | ||
|
|
||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,198 @@ | ||
| package com.example.sixth; | ||
|
|
||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import android.content.Context; | ||
| import android.database.Cursor; | ||
| import android.database.SQLException; | ||
| import android.database.sqlite.SQLiteDatabase; | ||
| import android.database.sqlite.SQLiteException; | ||
| import android.database.sqlite.SQLiteOpenHelper; | ||
| import android.os.Bundle; | ||
|
|
||
| public class DBHelper extends SQLiteOpenHelper { | ||
|
|
||
| private static String DB_PATH = "/data/data/com.example.sixth/databases/"; | ||
| private static String DB_NAME = "BarSample.db"; | ||
| private final Context myContext; | ||
| public static String tableName = "BARS"; | ||
| public String barTableName= Bar.barTableName; | ||
|
|
||
| String sqlquery; | ||
| //String firstBarTableName = MainActivity.upperCaseName;// Pass in the | ||
| // specific | ||
| // bar from | ||
| // the | ||
| // spinner | ||
| // choice | ||
| private SQLiteDatabase myDataBase; | ||
|
|
||
| public DBHelper(Context context) { | ||
|
|
||
| super(context, DB_NAME, null, 1); | ||
| this.myContext = context; | ||
| } | ||
|
|
||
|
|
||
| //Creates a empty database on the system and rewrites it with your own | ||
| // database. | ||
|
|
||
| public void createDataBase() throws IOException { | ||
|
|
||
| boolean dbExist = checkDataBase(); | ||
|
|
||
| if (dbExist) { | ||
| // do nothing - database already exist | ||
| } else { | ||
|
|
||
| // By calling this method and empty database will be created into | ||
| // the default system path | ||
| // of your application so we are gonna be able to overwrite that | ||
| // database with our database. | ||
| this.getReadableDatabase(); | ||
|
|
||
| try { | ||
| this.close(); | ||
| copyDataBase(); | ||
|
|
||
| } catch (IOException e) { | ||
|
|
||
| throw new Error("Error copying database"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| //Check if the database already exist to avoid re-copying the file each | ||
| // time you open the application. | ||
|
|
||
| // @return true if it exists, false if it doesn't | ||
|
|
||
| private boolean checkDataBase() { | ||
|
|
||
| SQLiteDatabase checkDB = null; | ||
|
|
||
| try { | ||
| String myPath = DB_PATH + DB_NAME; | ||
| checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | ||
|
|
||
| } catch (SQLiteException e) { | ||
| // database does't exist yet. | ||
| } | ||
| if (checkDB != null) { | ||
| checkDB.close(); | ||
| } | ||
| return checkDB != null ? true : false; | ||
| } | ||
|
|
||
|
|
||
| //Copies your database from your local assets-folder to the just created | ||
| //empty database in the system folder, from where it can be accessed and | ||
| // handled. This is done by transfering bytestream. | ||
|
|
||
| private void copyDataBase() throws IOException { | ||
|
|
||
| // Open your local db as the input stream | ||
| InputStream myInput = myContext.getAssets().open(DB_NAME); | ||
|
|
||
| // Path to the just created empty db | ||
| String outFileName = DB_PATH + DB_NAME; | ||
|
|
||
| // Open the empty db as the output stream | ||
| OutputStream myOutput = new FileOutputStream(outFileName); | ||
|
|
||
| // transfer bytes from the inputfile to the outputfile | ||
| byte[] buffer = new byte[1024]; | ||
| int length; | ||
| while ((length = myInput.read(buffer)) > 0) { | ||
| myOutput.write(buffer, 0, length); | ||
| } | ||
|
|
||
| // Close the streams | ||
| myOutput.flush(); | ||
| myOutput.close(); | ||
| myInput.close(); | ||
|
|
||
| } | ||
|
|
||
| public void openDataBase() throws SQLException { | ||
|
|
||
| // Open the database | ||
| String myPath = DB_PATH + DB_NAME; | ||
| myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public synchronized void close() { | ||
|
|
||
| if (myDataBase != null) | ||
| myDataBase.close(); | ||
|
|
||
| super.close(); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onCreate(SQLiteDatabase db) { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | ||
|
|
||
| } | ||
|
|
||
| public List<String> getAllLabels() { | ||
| List<String> labels = new ArrayList<String>(); | ||
|
|
||
| // Select All Query | ||
| String selectQuery = "SELECT * FROM " + tableName; | ||
|
|
||
| SQLiteDatabase db = this.getReadableDatabase(); | ||
| Cursor cursor = db.rawQuery(selectQuery, null); | ||
|
|
||
| // looping through all rows and adding to list | ||
| if (cursor.moveToFirst()) { | ||
| do { | ||
| labels.add(cursor.getString(1) + " " + cursor.getString(2) + ", " + cursor.getString(3)); | ||
| } while (cursor.moveToNext()); | ||
| } | ||
|
|
||
| // closing connection | ||
| cursor.close(); | ||
| db.close(); | ||
|
|
||
| // returning labels | ||
| return labels; | ||
|
|
||
| } // will returns all labels stored in database | ||
| public List<String> getBeerDrinkLabels(){ | ||
|
|
||
| List<String> allBeerDrinkLabels = new ArrayList<String>(); | ||
|
|
||
| String sqlquery="SELECT * FROM " + barTableName + " WHERE DRINKTYPE='Beer';"; | ||
| String selectQuery = sqlquery; | ||
|
|
||
| SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = | ||
| db.rawQuery(selectQuery, null); | ||
|
|
||
| // looping through all rows and adding to list if (cursor.moveToFirst()) | ||
| { do { allBeerDrinkLabels.add(cursor.getString(1) + " Price: " + | ||
| cursor.getString(2)); } while (cursor.moveToNext()); } | ||
|
|
||
| // closing connection | ||
| cursor.close(); | ||
| db.close(); | ||
|
|
||
| // returning labels | ||
| return allBeerDrinkLabels; | ||
|
|
||
| } // will returns all labels stored in database | ||
|
|
||
| } |
| @@ -0,0 +1,142 @@ | ||
| package com.example.sixth; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import android.app.Activity; | ||
| import android.os.Bundle; | ||
| import android.widget.ExpandableListView; | ||
|
|
||
|
|
||
| public class Drinks extends Activity { | ||
| // TextView drinkHolder; | ||
| String barName; | ||
| static String barNameHolder = MainActivity.upperCaseName; | ||
| String drinkTestHolder = ""; | ||
| ExpandableListAdapter listAdapter; | ||
| ExpandableListView expListView; | ||
| List<String> listDataHeader; | ||
| HashMap<String, List<String>> listDataChild; | ||
| List<String> beer; | ||
| //List<String> beerLables = MainActivity.beerLables; | ||
|
|
||
|
|
||
| @Override | ||
| public void onCreate(Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
| setContentView(R.layout.activity_drinks); | ||
| Bundle bundle = getIntent().getExtras(); | ||
| //if (bundle != null) { | ||
| // barNameHolder = bundle.getString("bar"); | ||
| // barName =barNameHolder; | ||
| //} | ||
| // get the listview | ||
|
|
||
| expListView = (ExpandableListView) findViewById(R.id.lvExp); | ||
|
|
||
| // preparing list data | ||
| prepareListData(); | ||
|
|
||
| listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); | ||
|
|
||
| // setting list adapter | ||
| expListView.setAdapter(listAdapter); | ||
|
|
||
| } | ||
|
|
||
| private void prepareListData() { | ||
| listDataHeader = new ArrayList<String>(); | ||
| listDataChild = new HashMap<String, List<String>>(); | ||
|
|
||
|
|
||
| // Adding child data | ||
|
|
||
| listDataHeader.add("Beer"); | ||
| listDataHeader.add("Mixed Drinks"); | ||
| listDataHeader.add("Other Drinks"); | ||
| listDataHeader.add("Shots"); | ||
| listDataHeader.add("Wine"); | ||
|
|
||
| DBHelper db = new DBHelper(getApplicationContext()); | ||
| List<String> beerLables = db.getBeerDrinkLabels(); | ||
| for (int i = 0; i < beerLables.size(); i++) { | ||
| beer.add(beerLables.get(i)); | ||
| } | ||
|
|
||
| /*if(barNameHolder.equals("CHANGOS")){ | ||
| List<String> beerLables ; | ||
| List<String> beer = new ArrayList<String>();; | ||
| beerLables = db.getChangosBeerDrinkLabels(); | ||
| for (int i = 0; i < beerLables.size(); i++) { | ||
| beer.add(beerLables.get(i)); | ||
| } | ||
| } | ||
| else if (barNameHolder.equals("LANDOS")){ | ||
| List<String> beerLables; | ||
| List<String> beer = new ArrayList<String>();; | ||
| beerLables = db.getLandosBeerDrinkLabels(); | ||
| for (int i = 0; i < beerLables.size(); i++) { | ||
| beer.add(beerLables.get(i)); | ||
| } | ||
| } | ||
| else { | ||
| List<String> beerLables; | ||
| List<String> beer = new ArrayList<String>();; | ||
| beerLables = db.getAnthonysBeerDrinkLabels(); | ||
| for (int i = 0; i < beerLables.size(); i++) { | ||
| beer.add(beerLables.get(i)); | ||
| } | ||
| }*/ | ||
| //Get Beer entries | ||
|
|
||
|
|
||
|
|
||
|
|
||
| // Adding child data | ||
|
|
||
|
|
||
| //beer.add("The Shawshank Redemption"); | ||
|
|
||
|
|
||
| /* | ||
| beer.add("The Godfather"); | ||
| * beer.add("The Godfather: Part II"); beer.add("Pulp Fiction"); | ||
| * beer.add("The Good, the Bad and the Ugly"); beer.add( | ||
| * "The Dark Knight"); beer.add("12 Angry Men"); | ||
| */ | ||
|
|
||
| List<String> wine = new ArrayList<String>(); | ||
| /* | ||
| * wine.add("The Conjuring"); wine.add("Despicable Me 2"); | ||
| * wine.add("Turbo"); wine.add("Grown Ups 2"); wine.add("Red 2"); | ||
| * wine.add("The Wolverine"); | ||
| */ | ||
|
|
||
| List<String> shots = new ArrayList<String>(); | ||
| /* | ||
| * shots.add("2 Guns"); shots.add("The Smurfs 2"); shots.add( | ||
| * "The Spectacular Now"); shots.add("The Canyons"); shots.add( | ||
| * "Europa Report"); | ||
| */ | ||
|
|
||
| List<String> mixedDrinks = new ArrayList<String>(); | ||
| /* | ||
| * mixedDrinks.add("2 Guns"); mixedDrinks.add("The Smurfs 2"); | ||
| * mixedDrinks.add("The Spectacular Now"); mixedDrinks.add("The Canyons" | ||
| * ); mixedDrinks.add("Europa Report"); | ||
| */ | ||
|
|
||
| List<String> otherDrinks = new ArrayList<String>(); | ||
| /* | ||
| * otherDrinks.add("2 Guns"); otherDrinks.add("The Smurfs 2"); | ||
| * otherDrinks.add("The Spectacular Now"); otherDrinks.add("The Canyons" | ||
| * ); otherDrinks.add("Europa Report"); | ||
| */ | ||
|
|
||
| listDataChild.put(listDataHeader.get(0), beer); // Header, Child data | ||
| listDataChild.put(listDataHeader.get(1), wine); | ||
| listDataChild.put(listDataHeader.get(2), shots); | ||
| listDataChild.put(listDataHeader.get(3), mixedDrinks); | ||
| listDataChild.put(listDataHeader.get(4), otherDrinks); | ||
| } | ||
| } |
| @@ -0,0 +1,133 @@ | ||
| package com.example.sixth; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import android.app.Activity; | ||
| import android.content.Intent; | ||
| import android.database.SQLException; | ||
| import android.os.Bundle; | ||
| import android.view.Menu; | ||
| import android.view.MenuItem; | ||
| import android.view.View; | ||
| import android.view.View.OnClickListener; | ||
| import android.widget.AdapterView; | ||
| import android.widget.AdapterView.OnItemSelectedListener; | ||
| import android.widget.ArrayAdapter; | ||
| import android.widget.Button; | ||
| import android.widget.Spinner; | ||
|
|
||
|
|
||
| public class MainActivity extends Activity implements | ||
| OnItemSelectedListener { | ||
| DBHelper myDB; | ||
| Button selectBar; | ||
| Spinner spinner; | ||
| String pullBar,setBar,name, cityState; | ||
| public String upperCaseName; | ||
|
|
||
| // static List<String> beer, beerLables; | ||
|
|
||
| @Override | ||
| public void onCreate(Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
| setContentView(R.layout.activity_main); | ||
| Button selectBar = (Button)findViewById(R.id.btnSelectBar); | ||
| myDB = new DBHelper(this); | ||
| // Spinner element | ||
| spinner = (Spinner) findViewById(R.id.spinner); | ||
| // Spinner click listener | ||
| spinner.setOnItemSelectedListener(this); | ||
|
|
||
| try { | ||
| myDB.createDataBase(); | ||
| } catch (IOException ioe) { | ||
| throw new Error("Unable to create database"); | ||
| } | ||
| try { | ||
| myDB.openDataBase(); | ||
| } catch (SQLException sqle) { | ||
| throw sqle; | ||
| } | ||
|
|
||
|
|
||
| // Loading spinner data from database | ||
| loadSpinnerData(); | ||
|
|
||
| selectBar.setOnClickListener(new OnClickListener() { | ||
| @Override | ||
| public void onClick(View v) { | ||
| pullBar = String.valueOf(spinner.getSelectedItem()); | ||
| String[] parts = pullBar.split(" "); //returns an array with the 2 parts | ||
| name = parts[0]; | ||
|
|
||
| String nlast = parts[parts.length-2]; | ||
| String last = parts[parts.length-1]; | ||
|
|
||
| cityState = nlast+ " " + last; | ||
| upperCaseName=name.toUpperCase(); | ||
| setBar = name.toLowerCase(); | ||
|
|
||
|
|
||
| DBHelper.barTableName = upperCaseName; | ||
| Intent i = (new Intent(MainActivity.this, Bar.class)); | ||
| //i.putExtra("bar",upperCaseName); | ||
| startActivity(i); | ||
| } | ||
|
|
||
| }); | ||
| } | ||
|
|
||
|
|
||
| private void loadSpinnerData() { | ||
| // database handler | ||
| DBHelper db = new DBHelper(getApplicationContext()); | ||
|
|
||
| // Spinner Drop down elements | ||
| List<String> lables = db.getAllLabels(); | ||
|
|
||
| // Creating adapter for spinner | ||
| ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, | ||
| android.R.layout.simple_spinner_item, lables); | ||
|
|
||
| // Drop down layout style - list view with radio button | ||
| dataAdapter | ||
| .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | ||
|
|
||
| // attaching data adapter to spinner | ||
| spinner.setAdapter(dataAdapter); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean onCreateOptionsMenu(Menu menu) { | ||
| // Inflate the menu; this adds items to the action bar if it is present. | ||
| getMenuInflater().inflate(R.menu.main, menu); | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean onOptionsItemSelected(MenuItem item) { | ||
| // Handle action bar item clicks here. The action bar will | ||
| // automatically handle clicks on the Home/Up button, so long | ||
| // as you specify a parent activity in AndroidManifest.xml. | ||
| int id = item.getItemId(); | ||
| if (id == R.id.action_settings) { | ||
| return true; | ||
| } | ||
| return super.onOptionsItemSelected(item); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { | ||
| // TODO Auto-generated method stub | ||
|
|
||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public void onNothingSelected(AdapterView<?> parent) { | ||
| // TODO Auto-generated method stub | ||
|
|
||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,109 @@ | ||
| package com.example.sixth; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import android.database.Cursor; | ||
| import android.database.sqlite.SQLiteDatabase; | ||
|
|
||
| public class testing { | ||
|
|
||
| /*public List<String> getAnthonysBeerDrinkLabels() { | ||
| List<String> allBeerDrinkLabels = new ArrayList<String>(); | ||
| // Select All Query | ||
| String sqlBeerquery = "SELECT * FROM ANTHONYS WHERE DRINKTYPE='Beer';"; | ||
| SQLiteDatabase bdb = this.getReadableDatabase(); | ||
| Cursor beerCursor = bdb.rawQuery(sqlBeerquery, null); | ||
|
|
||
| // looping through all rows and adding to list | ||
| if (beerCursor.moveToFirst()) { | ||
| do { | ||
| allBeerDrinkLabels.add(beerCursor.getString(1) + ", " + beerCursor.getString(2)); | ||
| } while (beerCursor.moveToNext()); | ||
| } | ||
|
|
||
| // closing connection | ||
| beerCursor.close(); | ||
| bdb.close(); | ||
|
|
||
| // returning labels | ||
| return allBeerDrinkLabels; | ||
|
|
||
| } // will returns all labels stored in database | ||
|
|
||
| public List<String> getAnthonysWineDrinkLabels() { | ||
| List<String> allWineDrinkLabels = new ArrayList<String>(); | ||
| String sqlWinequery = "SELECT * FROM ANTHONYS WHERE DRINKTYPE='Wine';"; | ||
| SQLiteDatabase wdb = this.getReadableDatabase(); | ||
| Cursor wineCursor = wdb.rawQuery(sqlWinequery, null); | ||
|
|
||
| // looping through all rows and adding to list | ||
| if (wineCursor.moveToFirst()) { | ||
| do { | ||
| allWineDrinkLabels.add(wineCursor.getString(1) + ", " + wineCursor.getString(2)); | ||
| } while (wineCursor.moveToNext()); | ||
| } | ||
|
|
||
| // closing connection | ||
| wineCursor.close(); | ||
| wdb.close(); | ||
| return allWineDrinkLabels; | ||
| } | ||
|
|
||
| public List<String> getAnthonysShotsDrinkLabels() { | ||
| List<String> allShotsDrinkLabels = new ArrayList<String>(); | ||
| String sqlShotsquery = "SELECT * FROM ANTHONYS WHERE DRINKTYPE='Shot';"; | ||
| SQLiteDatabase sdb = this.getReadableDatabase(); | ||
| Cursor shotCursor = sdb.rawQuery(sqlShotsquery, null); | ||
|
|
||
| // looping through all rows and adding to list | ||
| if (shotCursor.moveToFirst()) { | ||
| do { | ||
| allShotsDrinkLabels.add(shotCursor.getString(1) + ", " + shotCursor.getString(2)); | ||
| } while (shotCursor.moveToNext()); | ||
| } | ||
|
|
||
| // closing connection | ||
| shotCursor.close(); | ||
| sdb.close(); | ||
| return allShotsDrinkLabels; | ||
| } | ||
|
|
||
| public List<String> getAnthonysMDDrinkLabels() { | ||
| List<String> allMDDrinkLabels = new ArrayList<String>(); | ||
| String sqlMDquery = "SELECT * FROM ANTHONYS WHERE DRINKTYPE='Mixed Drink';"; | ||
| SQLiteDatabase mddb = this.getReadableDatabase(); | ||
| Cursor mDCursor = mddb.rawQuery(sqlMDquery, null); | ||
|
|
||
| // looping through all rows and adding to list | ||
| if (mDCursor.moveToFirst()) { | ||
| do { | ||
| allMDDrinkLabels.add(mDCursor.getString(1) + ", " + mDCursor.getString(2)); | ||
| } while (mDCursor.moveToNext()); | ||
| } | ||
|
|
||
| // closing connection | ||
| mDCursor.close(); | ||
| mddb.close(); | ||
| return allMDDrinkLabels; | ||
| } | ||
|
|
||
| public List<String> getAnthonysOtherDrinkLabels() { | ||
| List<String> allOtherDrinkLabels = new ArrayList<String>(); | ||
| String sqlOtherquery = "SELECT * FROM ANTHONYS WHERE DRINKTYPE='Other';"; | ||
| SQLiteDatabase odb = this.getReadableDatabase(); | ||
| Cursor otherCursor = odb.rawQuery(sqlOtherquery, null); | ||
|
|
||
| // looping through all rows and adding to list | ||
| if (otherCursor.moveToFirst()) { | ||
| do { | ||
| allOtherDrinkLabels.add(otherCursor.getString(1) + ", " + otherCursor.getString(2)); | ||
| } while (otherCursor.moveToNext()); | ||
| } | ||
|
|
||
| // closing connection | ||
| otherCursor.close(); | ||
| odb.close(); | ||
| return allOtherDrinkLabels; | ||
| }*/ | ||
| } |
| @@ -0,0 +1,133 @@ | ||
| package com.example.sixth; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import android.app.Activity; | ||
| import android.content.Intent; | ||
| import android.database.SQLException; | ||
| import android.os.Bundle; | ||
| import android.view.Menu; | ||
| import android.view.MenuItem; | ||
| import android.view.View; | ||
| import android.view.View.OnClickListener; | ||
| import android.widget.AdapterView; | ||
| import android.widget.AdapterView.OnItemSelectedListener; | ||
| import android.widget.ArrayAdapter; | ||
| import android.widget.Button; | ||
| import android.widget.Spinner; | ||
|
|
||
|
|
||
| public class MainActivity extends Activity implements | ||
| OnItemSelectedListener { | ||
| DBHelper myDB; | ||
| Button selectBar; | ||
| Spinner spinner; | ||
| String pullBar,setBar,name, cityState; | ||
| public String upperCaseName; | ||
|
|
||
| // static List<String> beer, beerLables; | ||
|
|
||
| @Override | ||
| public void onCreate(Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
| setContentView(R.layout.activity_main); | ||
| Button selectBar = (Button)findViewById(R.id.btnSelectBar); | ||
| myDB = new DBHelper(this); | ||
| // Spinner element | ||
| spinner = (Spinner) findViewById(R.id.spinner); | ||
| // Spinner click listener | ||
| spinner.setOnItemSelectedListener(this); | ||
|
|
||
| try { | ||
| myDB.createDataBase(); | ||
| } catch (IOException ioe) { | ||
| throw new Error("Unable to create database"); | ||
| } | ||
| try { | ||
| myDB.openDataBase(); | ||
| } catch (SQLException sqle) { | ||
| throw sqle; | ||
| } | ||
|
|
||
|
|
||
| // Loading spinner data from database | ||
| loadSpinnerData(); | ||
|
|
||
| selectBar.setOnClickListener(new OnClickListener() { | ||
| @Override | ||
| public void onClick(View v) { | ||
| pullBar = String.valueOf(spinner.getSelectedItem()); | ||
| String[] parts = pullBar.split(" "); //returns an array with the 2 parts | ||
| name = parts[0]; | ||
|
|
||
| String nlast = parts[parts.length-2]; | ||
| String last = parts[parts.length-1]; | ||
|
|
||
| cityState = nlast+ " " + last; | ||
| upperCaseName=name.toUpperCase(); | ||
| setBar = name.toLowerCase(); | ||
|
|
||
|
|
||
| DBHelper.barTableName = upperCaseName; | ||
| Intent i = (new Intent(MainActivity.this, Bar.class)); | ||
| //i.putExtra("bar",upperCaseName); | ||
| startActivity(i); | ||
| } | ||
|
|
||
| }); | ||
| } | ||
|
|
||
|
|
||
| private void loadSpinnerData() { | ||
| // database handler | ||
| DBHelper db = new DBHelper(getApplicationContext()); | ||
|
|
||
| // Spinner Drop down elements | ||
| List<String> lables = db.getAllLabels(); | ||
|
|
||
| // Creating adapter for spinner | ||
| ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, | ||
| android.R.layout.simple_spinner_item, lables); | ||
|
|
||
| // Drop down layout style - list view with radio button | ||
| dataAdapter | ||
| .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | ||
|
|
||
| // attaching data adapter to spinner | ||
| spinner.setAdapter(dataAdapter); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean onCreateOptionsMenu(Menu menu) { | ||
| // Inflate the menu; this adds items to the action bar if it is present. | ||
| getMenuInflater().inflate(R.menu.main, menu); | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean onOptionsItemSelected(MenuItem item) { | ||
| // Handle action bar item clicks here. The action bar will | ||
| // automatically handle clicks on the Home/Up button, so long | ||
| // as you specify a parent activity in AndroidManifest.xml. | ||
| int id = item.getItemId(); | ||
| if (id == R.id.action_settings) { | ||
| return true; | ||
| } | ||
| return super.onOptionsItemSelected(item); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { | ||
| // TODO Auto-generated method stub | ||
|
|
||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public void onNothingSelected(AdapterView<?> parent) { | ||
| // TODO Auto-generated method stub | ||
|
|
||
| } | ||
|
|
||
| } |