package com.gourmetMeal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.gally_3d.ShowAllPic;
import com.gourmetMeal.database.DataShare;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
/**
* 主菜单界面
*
* @author bcu
*
*/
public class MenuActivity extends CommonActivity implements
android.view.View.OnClickListener,OnItemSelectedListener,OnChildClickListener{
private Bitmap bit;
/**
* 下拉菜单
*/
protected Spinner menu_spinner_type = null;
/**
* 菜单图片
*/
protected ImageView menu_img_caret = null;
/**
* 价格
*/
protected TextView menu_txt_value = null;
/**
* 名称
*/
protected TextView menu_txt_name = null;
/**
* 菜品介绍
*/
protected TextView menu_txt_caretintroduct=null;
/**
* 返回按钮
*/
protected Button menu_btn_back = null;
/**
* 下一步按钮
*/
private Button menu_btn_next = null;
/**
* 点菜按钮
*/
private Button menu_btn_caret = null;
// /**
// * 菜品介绍按钮
// */
// private Button menu_btn_introduce = null;
// /**
// * 放大按钮
// */
// private ImageButton menu_ibtn_more = null;
/**
* 展开始菜单
*/
protected ExpandableListView menu_expList = null;
/**
* 跳转
*/
protected Intent menu_intent = null;
/**
* 菜名
*/
protected String menu_caiming = null;
/**
* 图片
*/
protected String menu_imageres;
/**
* 下拉菜单的选择项
*/
protected int menu_spinnerselect=0;
/**
* 展开菜单的计数
*/
protected int groupCount=0;
private SimpleExpandableListAdapter adapter=null;
protected Cursor cursor2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
infoUI(); //初始化UI
}
protected void infoUI() {
menu_spinner_type = (Spinner) findViewById(R.id.menu_spinner_type);
menu_img_caret = (ImageView) findViewById(R.id.menu_img_caret);
menu_txt_value=(TextView) findViewById(R.id.menu_txt_value);
menu_txt_name=(TextView) findViewById(R.id.menu_txt_name);
menu_txt_caretintroduct=(TextView) findViewById(R.id.menu_txt_caretintroduct);
menu_btn_next = (Button) findViewById(R.id.menu_btn_next);
menu_btn_caret = (Button) findViewById(R.id.menu_btn_caret);
menu_btn_back=(Button)findViewById(R.id.menu_btn_back);
menu_expList = (ExpandableListView) findViewById(R.id.menu_expList);
menu_intent = new Intent();
menu_spinner_type.setAdapter(new ArrayAdapter(MenuActivity.this,
R.layout.style_menu_spinner, R.id.menu_spinner_txt_value,
new String[] { "凉菜", "热菜", "主食", "酒类", "汤类", "饮料" ,"碗筷"}));
menu_spinner_type.setOnItemSelectedListener(this);
menu_btn_next.setOnClickListener(MenuActivity.this);
menu_btn_caret.setOnClickListener(MenuActivity.this);
menu_btn_back.setOnClickListener(MenuActivity.this);
menu_expList.setOnChildClickListener(MenuActivity.this);
menu_expList.setGroupIndicator(null);
cursor = database.rawQuery("SELECT caiming FROM liangcai", null);
cursor2= database.rawQuery("SELECT caiming FROM liangcai", null);
onCreateExpList(); //创建展开式菜单
}
/**
* 创建展开式菜单
*/
protected void onCreateExpList() {
// 创建二个一级条目标题
Map<String, String> title_1 = new HashMap<String, String>();
Map<String, String> title_2 = new HashMap<String, String>();
title_1.put("group", "特色菜推荐");
title_2.put("group", "菜单");
// 创建一级条目容器
List<Map<String, String>> gruops = new ArrayList<Map<String, String>>();
gruops.add(title_1);
gruops.add(title_2);
// 创建二级条目内容
// 内容一
List<Map<String, String>> childs_1 = new ArrayList<Map<String, String>>();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
menu_caiming = cursor.getString(0);
Map<String, String> title_1_content = new HashMap<String, String>();
title_1_content.put("child", menu_caiming);
childs_1.add(title_1_content);
cursor.moveToNext();
}
// 内容二
List<Map<String, String>> childs_2 = new ArrayList<Map<String, String>>();
cursor2.moveToFirst();
while (!cursor2.isAfterLast()) {
menu_caiming = cursor2.getString(0);
Map<String, String> title_2_content = new HashMap<String, String>();
title_2_content.put("child", menu_caiming);
childs_2.add(title_2_content);
cursor2.moveToNext();
}
// 存放两个内容, 以便显示在列表中
List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
childs.add(childs_1);
childs.add(childs_2);
adapter = new SimpleExpandableListAdapter(
MenuActivity.this, gruops, R.layout.style_menu_list_group,
new String[] { "group" },
new int[] { R.id.menu_list_group_value }, childs,
R.layout.style_menu_list_child, new String[] { "child" },
new int[] { R.id.menu_list_child_value });
menu_expList.setAdapter(adapter); // 加载适配器
groupCount = menu_expList.getCount();
for (int i = 0; i < groupCount; i++) {
menu_expList.expandGroup(i);
} }
case R.id.menu_btn_next:
menu_intent.setClass(MenuActivity.this, ListActivity.class);
startActivity(menu_intent);
menu_intent=null;
this.finish();
break;
case R.id.menu_btn_back
menu_intent.setClass(MenuActivity.this, MainActivity.class);
startActivity(menu_intent);
this.finish();
break;
case R.id.menu_btn_caret:
if(foodname.equals(null) || foodname.equals(""))
{
Toast.makeText(this, "请点菜",Toast.LENGTH_LONG).show();
break;
}
else {
theFood=sqlfood(); //加载菜品信息
int index = DataShare.isExistGoods(foodid); //获取检索值
if (index != -1) { // 已添加过该商品
DataShare.shopList.get(index).setNum(
DataShare.shopList.get(index).getNum() + 1); //菜品数量加1
DataShare.shopList.get(index).setJiage(DataShare.shopList.get(index).getNum()*DataShare.shopList.get(index).getDanjia()); //菜品价格=单价*数量
} else {
theFood.setNum(1); //该类添加第一个
theFood.setJiage(fooddanjia); //单价即为总价
DataShare.shopList.add(theFood); //加入列表当中
}
Toast.makeText(this,"您选了"+foodname,Toast.LENGTH_LONG).show(); //弹出提示
break; } }}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
menu_spinnerselect = arg2+1; //获取点击焦点
switch (arg2) {
case 0: //凉菜
cursor = database.rawQuery("SELECT caiming FROM liangcai WHERE tejiacai=1", null);
cursor2 = database.rawQuery("SELECT caiming FROM liangcai WHERE tejiacai=0", null);
onCreateExpList(); // 加载展开式菜单
break;
case 1://热菜
cursor = database.rawQuery("SELECT caiming FROM recai WHERE tejiacai=1", null);
cursor2 = database.rawQuery("SELECT caiming FROM recai WHERE tejiacai=0", null);
onCreateExpList();// 加载展开式菜单
break;
case 2: //主食
cursor = database.rawQuery("SELECT caiming FROM zhushi WHERE tejiacai=1", null);
cursor2 = database.rawQuery("SELECT caiming FROM zhushi WHERE tejiacai=0", null);
onCreateExpList(); // 加载展开式菜单
break;
case 3: //酒类
cursor = database.rawQuery("SELECT caiming FROM jiulei WHERE tejiacai=1", null);
cursor2 = database.rawQuery("SELECT caiming FROM jiulei WHERE tejiacai=0", null);
onCreateExpList(); // 加载展开式菜单
break;
case 4: //汤类
cursor = database.rawQuery("SELECT caiming FROM tanglei WHERE tejiacai=1", null);
cursor2 = database.rawQuery("SELECT caiming FROM tanglei WHERE tejiacai=0", null);
onCreateExpList();
break;
case 5: //饮料
cursor = database.rawQuery("SELECT caiming FROM yinliao WHERE tejiacai=1", null);
cursor2 = database.rawQuery("SELECT caiming FROM yinliao WHERE tejiacai=0", null);
onCreateExpList(); // 加载展开式菜单
break;
case 6: //碗筷
cursor = database.rawQuery("SELECT caiming FROM wankuai WHERE tejiacai=1", null);
cursor2 = database.rawQuery("SELECT caiming FROM wankuai WHERE tejiacai=0", null);
onCreateExpList(); // 加载展开式菜单
break;
};}
public void onNothingSelected(AdapterView<?> arg0) { }
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
switch (groupPosition) {
case 0: //特价菜
cursor = database.rawQuery("SELECT tupian,caiming,jiage,chanpinshuoming FROM caidan WHERE caipinbianhao="+menu_spinnerselect+"0"+(childPosition+1), null);
cursor.moveToFirst(); //游标指向初始位置
menu_imageres=cursor.getString(0); //保存图片地址
foodname=cursor.getString(1); //获得菜名
menu_txt_name.setText(cursor.getString(1)); //设置菜名
menu_txt_value.setText(cursor.getString(2)+"元"); //设置价格
menu_txt_caretintroduct.setText(cursor.getString(3)); //设置菜品介绍
System.out.println("http://192.168.1.202:8080/GourmetOrderServer/"+menu_imageres);
bit=ShowAllPic.GetNetBitmap("http://192.168.1.202:8080/GourmetOrderServer/"+menu_imageres);
menu_img_caret.setImageBitmap(bit);
CommonActivity.menu_imgres=bit;
break;
case 1: //菜单
cursor = database.rawQuery("SELECT tupian,caiming,jiage,chanpinshuoming FROM caidan WHERE caipinbianhao="+menu_spinnerselect+"0"+(childPosition+1), null);
cursor.moveToFirst(); //游标指向初始位置
menu_imageres=cursor.getString(0); //保存图片地址
foodname=cursor.getString(1);//获得菜名
menu_txt_name.setText(cursor.getString(1)); //设置菜名
menu_txt_value.setText(cursor.getString(2)+"元");//设置价格
menu_txt_caretintroduct.setText(cursor.getString(3));//设置菜品介绍
bit=ShowAllPic.GetNetBitmap("http://192.168.1.202:8080/GourmetOrderServer/"+menu_imageres);
menu_img_caret.setImageBitmap(bit);
CommonActivity.menu_imgres=bit;
break;
}
return false; }
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
menu_intent.setClass(MenuActivity.this, MainActivity.class);
startActivity(menu_intent);
this.finish(); }
return false; } }