Back to project page foodroid.
The source code is released under:
GNU General Public License
If you think the Android project foodroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.wmc.ReservationClient; /* www. j av a 2s . co m*/ import java.io.FileInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPut; import org.apache.http.impl.client.DefaultHttpClient; import android.app.Activity; import android.app.TimePickerDialog; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnFocusChangeListener; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.ListView; import android.widget.Spinner; import android.widget.TextView; import android.widget.TimePicker; import android.widget.Toast; import com.sba.util.*; public class FoodList extends Activity { public static String Branch_ID = "com.wmc.ReservationClient.Branch_ID"; public static String Order_ID = "com.wmc.ReservationClient.Order_ID"; public static String EnableOrderBtn ="com.wmc.ReservationClient.EnableOrderBtn"; ListView FoodList; Spinner tableSpinner; CheckBox tableCheck; String branchID; private List<Food> foods=new ArrayList<Food>(); private List<Table> tables=new ArrayList<Table>(); DatabaseHelper db; ArrayAdapter<String> adapterTable; String Server; Button orederBtn, foodTimeBtn, tableTimeBtn; Calendar dateAndTime=Calendar.getInstance(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.foodlist); FoodList = (ListView)findViewById(R.id.listViewFoods); FoodList.setCacheColorHint(Color.TRANSPARENT); FoodList.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) { Intent foodpage = new Intent(FoodList.this, FoodPage.class); foodpage.putExtra("FoodID", foods.get(position).ID+""); startActivity(foodpage); } }); tableSpinner = (Spinner)findViewById(R.id.spinnerTable); tableCheck = (CheckBox)findViewById(R.id.checkBoxReserveTable); orederBtn = (Button)findViewById(R.id.buttonOrder); foodTimeBtn = (Button)findViewById(R.id.buttonFoodTime); tableTimeBtn = (Button)findViewById(R.id.buttonTableTime); branchID = getIntent().getExtras().getString(Branch_ID); if(getIntent().getExtras().getString(EnableOrderBtn) != null) { EnableOrderBtn = getIntent().getExtras().getString(EnableOrderBtn); Order_ID = getIntent().getExtras().getString(Order_ID); orederBtn.setVisibility(View.INVISIBLE); tableCheck.setEnabled(false); tableSpinner.setEnabled(false); foodTimeBtn.setEnabled(false); tableTimeBtn.setEnabled(false); } Server = getString(R.string.Server); try { db = new DatabaseHelper(this); LoadFood(); LoadTable(); } catch (Exception e) { e.printStackTrace(); } } public void onPause() { super.onPause(); EnableOrderBtn = "true"; } private void LoadTable() { adapterTable = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item); adapterTable.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); Cursor tableCursor; if(EnableOrderBtn.equals("false")) { String[] select = {"foodtime","tabletime"}; tableCursor = db.SelectTable("tbl_order", select, "ID=" + Order_ID, ""); tableCursor.moveToFirst(); foodTimeBtn.setText(tableCursor.getString(0)); tableTimeBtn.setText(tableCursor.getString(1)); } String[] select = {"ID","Name","Size"}; if(EnableOrderBtn.equals("false")) //tableCursor = db.SelectTable("Vw_orderrestable", select, "BranchID=" + branchID + " and OrderID=" + Order_ID, "Name"); tableCursor = db.SelectTable("Vw_orderrestable", select, "OrderID=" + Order_ID, "ID"); else //tableCursor = db.SelectTable("restable", select, "BranchID=" + branchID, "Name"); tableCursor = db.SelectTable("restable", select, "", "ID"); tableCursor.moveToFirst(); if(EnableOrderBtn.equals("false") && tableCursor.getCount()>0) tableCheck.setChecked(true); for(int i=0;i<tableCursor.getCount();i++) { Table newTable = new Table(); newTable.ID = tableCursor.getInt(0); newTable.Name = tableCursor.getString(1); newTable.Size = tableCursor.getInt(2); tables.add(newTable); adapterTable.add(newTable.Name + " - " + newTable.Size + " ?????"); tableCursor.moveToNext(); } tableSpinner.setAdapter(adapterTable); } void LoadFood() { try { Cursor foodCursor; if(EnableOrderBtn.equals("false")) { String[] select = {"ID","Name","Price","Picture","FoodNum"}; //foodCursor = db.SelectTable("Vw_orderfood", select, "BranchID=" + branchID + " and OrderID=" + Order_ID, "Name"); foodCursor = db.SelectTable("Vw_orderfood", select, "OrderID=" + Order_ID, "ID"); } else { String[] select2 = {"ID","Name","Price","Picture"}; //foodCursor = db.SelectTable("food", select2, "BranchID=" + branchID, "Name"); foodCursor = db.SelectTable("food", select2, "", "ID"); } foodCursor.moveToFirst(); for(int i=0;i<foodCursor.getCount();i++) { Food newFood = new Food(); newFood.ID = "" + foodCursor.getInt(0); newFood.Name = foodCursor.getString(1); newFood.Price = foodCursor.getString(2); newFood.Picture = foodCursor.getString(3); if(EnableOrderBtn.equals("false")) { newFood.Num = foodCursor.getInt(4); if(newFood.Num>0) newFood.IsChecked = true; } foods.add(newFood); foodCursor.moveToNext(); } FoodList.setAdapter(new IconicAdapter(this)); } catch (Exception e) { e.printStackTrace(); } } private class IconicAdapter extends BaseAdapter//ArrayAdapter<String> { private LayoutInflater mInflater; private Bitmap bmp; public IconicAdapter(Context context) { mInflater = LayoutInflater.from(context); } public int getCount() { return foods.size(); } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { try { ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.rowfood, null); holder.txtNum = (EditText) convertView.findViewById(R.id.editTextFoodNum); holder.img = (ImageView) convertView.findViewById(R.id.iconFoodList); holder.chk = (CheckBox) convertView.findViewById(R.id.checkBoxFoodSelect); holder.txt = (TextView) convertView.findViewById(R.id.labelFoodName); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } //Fill EditText with the value you have in data source Food food=foods.get(position); String urlImage = "/data/data/com.wmc.ReservationClient/" + food.Picture; InputStream is = new FileInputStream(urlImage); bmp = BitmapFactory.decodeStream(is); holder.img.setImageBitmap(bmp); holder.txt.setText(foods.get(position).Name + "\n" + "???? : " + foods.get(position).Price); if(EnableOrderBtn.equals("false")) { holder.chk.setChecked(foods.get(position).IsChecked); holder.chk.setEnabled(false); holder.txtNum.setText("" + foods.get(position).Num); holder.txtNum.setEnabled(false); } holder.txtNum.setId(position); holder.img.setId(position); holder.txt.setId(position); holder.chk.setId(position); //we need to update adapter once we finish with editing holder.txtNum.setOnFocusChangeListener(new OnFocusChangeListener() { public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus && !EnableOrderBtn.equals("false")) { final int position = v.getId(); final EditText TxtNum = (EditText) v; if(!TxtNum.getText().toString().equals("")) foods.get(position).Num = Integer.parseInt(TxtNum.getText().toString()); } } }); holder.chk.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(!EnableOrderBtn.equals("false")) { final int position = buttonView.getId(); foods.get(position).IsChecked = isChecked; } } }); } catch(Exception ex) { ex.printStackTrace(); } return convertView; } } static class ViewHolder { ImageView img; CheckBox chk; TextView txt; EditText txtNum; } public void onClick(View v) { switch(v.getId()) { case R.id.buttonFoodTime: new TimePickerDialog(FoodList.this, foodTime, dateAndTime.get(Calendar.HOUR_OF_DAY), dateAndTime.get(Calendar.MINUTE), true) .show(); break; case R.id.buttonTableTime: new TimePickerDialog(FoodList.this, tableTime, dateAndTime.get(Calendar.HOUR_OF_DAY), dateAndTime.get(Calendar.MINUTE), true) .show(); break; case R.id.buttonOrder: try { tableCheck.requestFocus(); Date datetime = new Date(); //int hour = Integer.parseInt(foodTimeBtn.getText().subSequence(0, 2).toString()); int selectedFoodNum = 0; for(int i=0;i<foods.size();i++) { Food food = foods.get(i); if(food.IsChecked) { selectedFoodNum++; } } //if(!(hour < 23 || hour > 18) && !(hour < 16 || hour > 11) ) //Toast.makeText(this, "?? ??? ???? ?????? ???????? ??? ???",Toast.LENGTH_LONG ).show(); //else //{ if(selectedFoodNum > 0 || tableCheck.isChecked()) { String foodForWebService = ""; double Sum = 0; String time = datetime.getHours() + ":" + datetime.getMinutes(); String foodTimeStr = foodTimeBtn.getText().toString(); String tableTimeStr = tableTimeBtn.getText().toString(); PersianCalendar c = new PersianCalendar(); DateFields df = c.getDateFields(); String date = df.getYear() + "/" + (df.getMonth()+1) + "/" + df.getDay(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); String username = prefs.getString("login", ""); for(int i=0;i<foods.size();i++) { Food food = foods.get(i); if(food.IsChecked) { if(food.Num > 10) { Toast.makeText(this, "????? ??? ?? ??? ????? ??? ?? 10 ??? ????",Toast.LENGTH_LONG ).show(); throw new Exception(""); } foodForWebService = foodForWebService + "_" + food.ID + "-" + food.Num; Sum = Sum + (food.Num * Double.parseDouble(food.Price)); } } //Local Update String[] fields = {"UserID","BranchID","Date","Time","Price","tableTime","foodtime"}; String[] values = {Utility.getCurrentUserID(this),branchID,date, time,""+Sum, tableTimeStr, foodTimeStr}; long OrderID = db.InsertRow("tbl_order", fields, values); for(int i=0;i<foods.size();i++) { Food food = foods.get(i); if(food.IsChecked) { String[] fields2 = {"OrderID","FoodID","FoodNum"}; String[] values2 = {""+OrderID,food.ID,""+food.Num}; db.InsertRow("orderfood", fields2, values2); } } Table curTable = null; if(tableCheck.isChecked() && tables.size() > 0) { curTable = tables.get(tableSpinner.getSelectedItemPosition()); String[] fields3 = {"OrderID","TableID"}; String[] values3 = {""+OrderID,""+curTable.ID}; db.InsertRow("ordertable", fields3, values3); } //Server Update String order = username + "-" + branchID + "-" + date.replace('/', '_') + "-" + foodTimeStr; String table = (tableCheck.isChecked() && tables.size() > 0 ? ""+curTable.ID : "-"); String food = (selectedFoodNum>0 ? foodForWebService : "-"); String price = ""+Sum; if(Utility.isNetworkAvailable(this)) { try { HttpClient client=new DefaultHttpClient(); HttpPut putMethod=new HttpPut( "http://" + Server + "/RestaurantReservation/resources/order/" + order + "/" + food + "/" + table + "/" + price); client.execute(putMethod); } catch(Exception ex) { ex.printStackTrace(); } } else { Utility.sendSMS("InsertOrder/" + order + "/" + food + "/" + table + "/" + price, this); } //END Toast.makeText(this, "?????? ??? ?? ??????? ??? ??",Toast.LENGTH_LONG ).show(); finish(); } //} } catch(Exception ex) { ex.printStackTrace(); } break; } } TimePickerDialog.OnTimeSetListener foodTime=new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { dateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay); dateAndTime.set(Calendar.MINUTE, minute); Button buttonFoodTime = (Button)findViewById(R.id.buttonFoodTime); buttonFoodTime.setText(dateAndTime.getTime().getHours() + ":" + dateAndTime.getTime().getMinutes()); } }; TimePickerDialog.OnTimeSetListener tableTime=new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { dateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay); dateAndTime.set(Calendar.MINUTE, minute); Button buttonTableTime = (Button)findViewById(R.id.buttonTableTime); buttonTableTime.setText(dateAndTime.getTime().getHours() + ":" + dateAndTime.getTime().getMinutes()); } }; }