Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. package com.bb.android.breakingbread;
  2.  
  3. import android.app.Activity;
  4. import android.content.res.Resources;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.graphics.drawable.Drawable;
  8. import android.os.Bundle;
  9. import android.view.Menu;
  10. import android.view.MenuItem;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.ImageButton;
  14. import android.widget.ImageView;
  15. import android.widget.TableLayout;
  16. import android.widget.TableRow;
  17. import android.widget.TextView;
  18. import android.widget.Toast;
  19. import android.content.ContentValues;
  20. import android.content.Context;
  21. import android.database.sqlite.SQLiteDatabase;
  22. import android.database.sqlite.SQLiteOpenHelper;
  23.  
  24.  
  25. public class MenuActivity extends Activity {
  26.  
  27. SQLiteDatabase db;
  28. TableRow tableRow;
  29. private static final int NUM_COL=3;
  30. private static final int NUM_ROW =3;
  31. Button MenuButtons[][]=new Button[NUM_ROW][NUM_COL];
  32. String [][] MenuTable = new String [NUM_ROW][NUM_COL];
  33. String[][] Checker;
  34. double [][] Bill = new double [20][40];
  35.  
  36. @Override
  37. protected void onCreate(Bundle savedInstanceState) {
  38. super.onCreate(savedInstanceState);
  39. LayoutMenuSC();
  40. DBhelper db = new DBhelper(this);
  41. Checker=db.getMenu();
  42.  
  43. }
  44.  
  45. //============SET LAYOUT OF MENU PAGE (P2)=================================================
  46. public void LayoutMenuSC(){
  47. // layout the buttons on screen
  48. setContentView(R.layout.activitymenu);
  49. TableLayout table =(TableLayout) findViewById(R.id.MenuButtonTable); //need to link this table to xml
  50.  
  51. for (int row=0; row<NUM_ROW;row++){
  52. //Set new Row in Table
  53. TableRow tableRow= new TableRow(this);
  54. tableRow.setLayoutParams(new TableLayout.LayoutParams(
  55. TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT,1.0F));
  56. table.addView(tableRow);
  57.  
  58. for (int col=0; col<NUM_COL; col++){
  59. int x,y;
  60. float w;
  61. ImageButton ImButton=new ImageButton(this);
  62. ImButton.setLayoutParams(new TableRow.LayoutParams(
  63. TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1.0F));
  64.  
  65. Resources res = getResources();
  66. Drawable drawable = res.getDrawable(R.drawable.ic_launcher);
  67. //Drawable drawable= res.getDrawable("R.drawable." + Photopath);<--------------------
  68. ImButton.setBackground(drawable);
  69. tableRow.addView(ImButton);
  70. }
  71. }
  72. }
  73.  
  74. @Override
  75. public boolean onCreateOptionsMenu(Menu menu) {
  76. // Inflate the menu; this adds items to the action bar if it is present.
  77. getMenuInflater().inflate(R.menu.breaking_bread, menu);
  78. return true;
  79. }
  80.  
  81. @Override
  82. public boolean onOptionsItemSelected(MenuItem item) {
  83. // Handle action bar item clicks here. The action bar will
  84. // automatically handle clicks on the Home/Up button, so long
  85. // as you specify a parent activity in AndroidManifest.xml.
  86. int id = item.getItemId();
  87. if (id == R.id.action_settings) {
  88. return true;
  89. }
  90. return super.onOptionsItemSelected(item);
  91. }
  92. }
  93.  
  94. package com.bb.android.breakingbread;
  95.  
  96. import android.content.ContentValues;
  97. import android.content.Context;
  98. import android.database.Cursor;
  99. import android.database.sqlite.SQLiteDatabase;
  100. import android.database.sqlite.SQLiteOpenHelper;
  101. import android.util.Log;
  102. import android.widget.Toast;
  103. import java.util.LinkedList;
  104. import java.util.List;
  105.  
  106.  
  107. public class DBhelper extends SQLiteOpenHelper {
  108.  
  109. private static final String TAG= DBhelper.class.getSimpleName();
  110. public static final String DB_NAME="BreakingBreadDB6";
  111. public static final int DB_VERSION=1;
  112.  
  113. private static final String TABLE_MENU = "MenuTable";
  114. private static final String KEY_ID = "id";
  115. private static final String KEY_SW = "SW";
  116. private static final String KEY_PRICE = "Price";
  117. private static final String KEY_PHOTOPATH = "Photopath";
  118.  
  119. // constructor
  120. public DBhelper(Context context) {
  121. super(context, DB_NAME, null, DB_VERSION);
  122. }
  123.  
  124.  
  125. @Override
  126. public void onCreate(SQLiteDatabase db) {
  127. // SQL statement to create menu table
  128. String CREATE_MENU_TABLE = "CREATE TABLE IF NOT EXISTS "+ TABLE_MENU +" ( " + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
  129. KEY_SW + " TEXT, "+
  130. KEY_PRICE +" TEXT, " +
  131. KEY_PHOTOPATH+ " TEXT )";
  132.  
  133.  
  134. // create table
  135. db.execSQL(CREATE_MENU_TABLE);
  136.  
  137.  
  138. // populate table
  139. AddSWRow("BBQ","15","Path1");
  140. //AddSWRow("Spicy Turky",12,"Path2");
  141. //AddSWRow("Chicken Melt",20,"Path3");
  142.  
  143. }
  144.  
  145. @Override
  146. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  147. // Drop older items table if existed
  148. db.execSQL("DROP TABLE IF EXISTS " + TABLE_MENU);
  149. // create fresh menu table
  150. this.onCreate(db);
  151. }
  152.  
  153. //====================ADD SANDWICH TO TABLE===================
  154. public void AddSWRow (String SwName, String SwPrice, String SwPhoto){
  155.  
  156. // 1. get reference to writable DB
  157. SQLiteDatabase db =this.getWritableDatabase();
  158.  
  159. // 2. create ContentValues to add key "column"/value
  160. ContentValues values = new ContentValues();
  161. values.put(KEY_SW,SwName);
  162. values.put(KEY_PRICE, SwPrice);
  163. values.put(KEY_PHOTOPATH, SwPhoto);
  164.  
  165. // 3. insert
  166. db.insert(TABLE_MENU,null,values); // key/value -> keys = column names/ values = column values
  167.  
  168. // 4. close*/
  169. db.close();
  170. }
  171.  
  172. //===================GET MENU================================
  173. public String[][] getMenu() {
  174. // List<Menu> Menu = new LinkedList<Menu>();
  175. String MenuArray[][] = new String[50][4];
  176. //0. initiatlise array with EOF..
  177. for (int i=0; i<50;i++){
  178. for (int j=0; j<4;j++){
  179. MenuArray[i][j]="EOF";
  180. }
  181. }
  182.  
  183. // 1. build the query
  184. String query = "SELECT * FROM " + TABLE_MENU;
  185.  
  186. // 2. get reference to writable DB
  187. SQLiteDatabase db = this.getReadableDatabase();
  188. Cursor cursor = db.rawQuery(query, null);
  189.  
  190. // 3. go over each row
  191.  
  192. Menu menu = null;
  193. int i;
  194. i=0;
  195.  
  196. if (cursor != null){
  197. cursor.moveToFirst();
  198. /*while (cursor.isAfterLast() == false) {
  199. MenuArray[i][0]=Integer.parseInt(cursor.getString(0));
  200. MenuArray[i][1]=cursor.getString(1);
  201. MenuArray[i][2]=cursor.getString(2);
  202. MenuArray[i][3]=cursor.getString(3);
  203.  
  204. i++;
  205. cursor.moveToNext();
  206. }*/
  207. cursor.close();
  208. }
  209. return MenuArray;
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement