实例介绍
【实例截图】
【核心代码】
package food.test.zqz;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import food.test.R;
import food.test.domain.Food;
import food.test.util.DButil;
import food.test.wjf.ShopCart;
import food.test.wjf.ShopCartShowAll;
import food.test.zqz.util.GetNetWorkData;
public class ActivityShowFoodByType extends Activity {
// 用于存储需要显示的数据
private ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
// 用于接收服务端
private ArrayList<Food> serverFoods = new ArrayList<Food>();
private GridView gv = null;
public final static int SHOW_FOOD_DETAIL = 0;
public final static int CONFIRM_PURCHASE = 1;
public final static int LOOK_UP_SHOPPING = 2;
private DButil db;
// 用户在界面上选中的所有商品信息
public Food selectedFood = new Food();
/**
* 根据菜品类型,获取服务器端的菜品信息
*
* @return
*/
private ArrayList<Food> getServerFoodInfoByType(int flagType) {
serverFoods.clear();
HashMap<String, String> request = new HashMap<String, String>();
// 获得服务端数据"根据约定的字符串格式"
String resData = GetNetWorkData.getConnectionPost(
GetNetWorkData.URL_AllFood, request).toString();
// String resData =
// "[foodName=test;foodType=0;foodPrice=33.3f;foodImage=500008.jpg],[foodPrice=44;foodType=0;foodName=红烧肉;foodImage=500022.jpg]";
ArrayList<Food> serverAllFoods = GetNetWorkData
.decodeResponseData(resData);
Food oneFood;
Iterator<Food> it = serverAllFoods.iterator();
while (it.hasNext()) {
oneFood = it.next();
if (oneFood.foodType == flagType) {
serverFoods.add(oneFood);
}
}
return serverFoods;
}// end of getServerData()
/**
* 准备显示在界面的数据
*/
private void prepareView() {
data.clear();
// 获取要显示菜品的类型
Intent intent = this.getIntent();
int showType = intent.getFlags();
// 获取指定类型的菜品
ArrayList<Food> assignedFoods = getServerFoodInfoByType(showType);
// 设定界面显示数据
HashMap<String, Object> item;
for (Food food : assignedFoods) {
item = new HashMap<String, Object>();
item.put("price", "价格:" food.foodPrice);
item.put("name", "菜名:" food.foodName);
item.put("icon", food.foodImage);
data.add(item);
}
}// end of prepareView()
public void onCreate(Bundle saved) {
super.onCreate(saved);
setContentView(R.layout.show);
try {
gv = (GridView) this.findViewById(R.id.grid);
prepareView();
MyGridViewAdapter mgva = new MyGridViewAdapter(this);
gv.setAdapter(mgva);
// 监听GridView点击事件
gv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectedFood = serverFoods.get(position);
setTitle("测试选中事件---" selectedFood.foodId);
Log.e("----test selected---", "" selectedFood.foodId);
ActivityShowFoodByType.this.openOptionsMenu();
Log.e("test GridView onClickListener", "------");
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
protected void onRestart() {
super.onRestart();
Log.e("TabIntent------", "test--onRestart()" getIntent().getFlags());
}
protected void onResume() {
super.onResume();
prepareView();
Log.e("TabIntent------", "test--onResume()" getIntent().getFlags());
}
protected void onStart() {
super.onStart();
Log.e("TabIntent------", "test--onStart()" getIntent().getFlags());
}
protected void onStop() {
super.onStop();
Log.e("TabIntent------", "test--onStop()" getIntent().getFlags());
}
protected void onPause() {
super.onPause();
Log.e("TabIntent------", "test--onPause()" getIntent().getFlags());
}
/**
* 添加功能Menu键
*/
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(1, SHOW_FOOD_DETAIL, Menu.CATEGORY_SYSTEM, "菜品详情").setIcon(
R.drawable.zqz_menu_lookdetail);
menu.add(1, CONFIRM_PURCHASE, Menu.CATEGORY_SYSTEM, "加入购物车").setIcon(
R.drawable.zqz_menu_purchase);
return true;
}
/**
* 监听用户选中事件
*/
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case SHOW_FOOD_DETAIL:
// 跳转到详情显示页面
Intent intent = new Intent(ActivityShowFoodByType.this, FoodDetail.class);
intent.putExtra("foodId", selectedFood.foodId);
intent.putExtra("foodName", selectedFood.foodName);
intent.putExtra("foodDescri", selectedFood.foodDescri);
intent.putExtra("foodType", selectedFood.foodType);
intent.putExtra("foodPrice", selectedFood.foodPrice);
intent.putExtra("foodImage", selectedFood.foodImage);
startActivity(intent);
break;
case CONFIRM_PURCHASE:
Log.e("test==", "" CONFIRM_PURCHASE);
if (selectedFood.foodId != 0) {
// 跳转到购物车界面,并且提示用户输入要几份菜品
final View zqz_dialog = LayoutInflater.from(this).inflate(
R.layout.zqz_dialog, null);
// 提示用户输入数量
new AlertDialog.Builder(this).setTitle("选购").setIcon(
R.drawable.tap_vegetarian2).setView(zqz_dialog)
.setPositiveButton("订购", new OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
EditText etnumber = (EditText) zqz_dialog
.findViewById(R.id.zd_etnumber);
String etnumberStr = etnumber.getText()
.toString().trim();
if (!etnumberStr.equals("")
&& etnumberStr != null) {
db = new DButil(ActivityShowFoodByType.this);
ShopCart shopcart = new ShopCart();
shopcart.setFoodId(selectedFood.foodId);
shopcart.setFoodName(selectedFood.foodName);
shopcart
.setFoodPrice(selectedFood.foodPrice);
int foodnum = Integer.parseInt(etnumberStr);
shopcart.setFoodNum(foodnum);
// float sumprices = selectedFood.foodPrice
// * foodnum;
float sumprices = getTotalMoney(
selectedFood.foodPrice, foodnum);
shopcart.setSumPrices(sumprices);
db.addFood(shopcart);
db.close();
Log.e("-------- shopcart", "" shopcart);
Intent intent = new Intent(
ActivityShowFoodByType.this,
ShopCartShowAll.class);
Bundle extras = new Bundle();
extras
.putInt("foodId",
selectedFood.foodId);
extras.putFloat("foodPrice",
selectedFood.foodPrice);
extras.putInt("foodNumber", Integer
.parseInt(etnumberStr));
extras.putString("foodName",
selectedFood.foodName);
extras.putString("用户名", "");
extras.putInt("用户Id", 0);
intent.putExtras(extras);
startActivity(intent);
} else {
Toast.makeText(ActivityShowFoodByType.this,
"请输入数量", Toast.LENGTH_SHORT).show();
}
}
}).setNegativeButton("取消", new OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
}).create().show();
} else {
Toast.makeText(ActivityShowFoodByType.this, "请选中菜品",
Toast.LENGTH_LONG).show();
}
break;
}// end of switch
return true;
}// end of onOptionsItemSelected()
/**
* 计算(可能含小数位)总金额
*/
private float getTotalMoney(float price, int number) {
BigDecimal bd_price = new BigDecimal(price);
BigDecimal bd_number = new BigDecimal(number);
return bd_price.multiply(bd_number).floatValue();
}// end of getTotalMoney()
/**
* Bean对象,用来封装GridView中的一个数据
*
* @author zhuqiuzhu
*
*/
public final class ViewHolder {
public ImageView foodImagView;
public TextView foodNameView;
public TextView foodPriceView;
}
class MyGridViewAdapter extends BaseAdapter {
LayoutInflater mInflater = null;
public MyGridViewAdapter(Context ctx) {
mInflater = LayoutInflater.from(ctx);
}
// 返回需要显示的数据集合的size
public int getCount() {
return data.size();
}
// 返回指定集合位置的子项数据
public Object getItem(int position) {
return data.get(position);
}
// 返回指定集合子项的ID号
public long getItemId(int position) {
return position;
}
// 返回某一条数据的视图
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// 若GridView 传进来的代表上一行数据的视图参数arg1为空,代表当前是第一行数据
// 这个时候则解析代表一行数据的视图展现的xml文件zqz_gridview.xml
// 并且把视图里的控件和bean对象里的控件关联起来
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.zqz_gridview, null);
holder.foodImagView = (ImageView) convertView
.findViewById(R.id.zg_gridicon);
holder.foodNameView = (TextView) convertView
.findViewById(R.id.zg_gridname);
holder.foodPriceView = (TextView) convertView
.findViewById(R.id.zg_gridprice);
// 设置标识
convertView.setTag(holder);
} else {
// 如果传进来的arg1不为空,则我们可以沿用上一行数据的视图,
holder = (ViewHolder) convertView.getTag();
}
// 只是需要将数据替换成本行的数据即可
/*-------------图片的显示实现--------------*/
String fi = ((HashMap<String, Object>) data.get(position)).get(
"icon").toString();
String mUrl = GetNetWorkData.getAssignedFoodBitmapUrl(
GetNetWorkData.URL, fi);
GetNetWorkData.setAssignedFoodBimapView(mUrl, holder.foodImagView);
/*----------------------------------------*/
holder.foodNameView.setText(((HashMap<String, Object>) data
.get(position)).get("name").toString());
holder.foodPriceView.setText(((HashMap<String, Object>) data
.get(position)).get("price").toString());
return convertView;
}
}// end of MyGridViewAdapter
}
标签: 点餐
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论