Android Open Source - foodroid Food List






From Project

Back to project page foodroid.

License

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.

Java Source Code

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());
    }
  };
  
}




Java Source Code List

com.adp.ADPWsSample.java
com.adp.BalanceResult.java
com.adp.ChangePasswordResponse.java
com.adp.ChangePassword.java
com.adp.GetBalanceResponse.java
com.adp.GetBalance.java
com.adp.GetStatusResponse.java
com.adp.GetStatus.java
com.adp.IncomingMessage.java
com.adp.JaxRpcMessagingServiceServiceLocator.java
com.adp.JaxRpcMessagingServiceService.java
com.adp.JaxRpcMessagingService.java
com.adp.MessageObject.java
com.adp.MessagingServiceSoapBindingStub.java
com.adp.MultiAddressMessageObject.java
com.adp.OutgoingMessage.java
com.adp.ReceiveResponse.java
com.adp.ReceiveResult.java
com.adp.Receive.java
com.adp.ReportResponse.java
com.adp.ReportResult.java
com.adp.Report.java
com.adp.Result.java
com.adp.SendMultipleResponse.java
com.adp.SendMultiple.java
com.adp.SendResponse.java
com.adp.SendResult.java
com.adp.Send.java
com.adp.StatusReportResponse.java
com.adp.StatusReportResult.java
com.adp.StatusReportType0.java
com.adp.StatusReport.java
com.sba.util.DateFields.java
com.sba.util.PersianCalendar.java
com.wmc.Registration.BranchlistResource.java
com.wmc.Registration.CommentListBean.java
com.wmc.Registration.CommentResource.java
com.wmc.Registration.CommentlistResource.java
com.wmc.Registration.FoodListResource.java
com.wmc.Registration.LoginResource.java
com.wmc.Registration.OrderBean.java
com.wmc.Registration.OrderListBean.java
com.wmc.Registration.OrderResource.java
com.wmc.Registration.RegisterResource.java
com.wmc.Registration.ReserveListBean.java
com.wmc.Registration.Settings.java
com.wmc.ReservationClient.Account.java
com.wmc.ReservationClient.BranchList.java
com.wmc.ReservationClient.BranchPage.java
com.wmc.ReservationClient.Branch.java
com.wmc.ReservationClient.Comment.java
com.wmc.ReservationClient.DatabaseHelper.java
com.wmc.ReservationClient.Favorite.java
com.wmc.ReservationClient.FoodList.java
com.wmc.ReservationClient.FoodPage.java
com.wmc.ReservationClient.Food.java
com.wmc.ReservationClient.Login.java
com.wmc.ReservationClient.Main.java
com.wmc.ReservationClient.OrderList.java
com.wmc.ReservationClient.Order.java
com.wmc.ReservationClient.Search.java
com.wmc.ReservationClient.SmsMessageReceiver.java
com.wmc.ReservationClient.Table.java
com.wmc.ReservationClient.Update.java
com.wmc.ReservationClient.Utility.java
smsserver.CommentSMS.java
smsserver.LoginSMS.java
smsserver.OrderSMS.java
smsserver.Settings.java
smsserver.SmsServer.java