【Android】线上自助点餐系统
- 效果视频
- 商品选购界面
- 效果图
- 商品数据
- 商品标题文件
- 商品详细文件信息
- 商品图片
- 添加商品数据
- 解析左侧标题数据
- 解析右侧商品数据
- 左侧标题与右侧商品双向绑定
- 加减按钮监听
- 所选商品存入数据库
- 商品复选
- 效果图
- 取出商品数据
- 商品清空
- 商品增删
- 支付界面
- 效果图
- 密码匹配
- 支付成功
- 效果图
- SharedPreferences
- 历史数据图表化
- 效果图
- 数据初始化
- X轴
- Y轴
- 尾言
效果视频
【Android】线上自助点餐系统
商品选购界面
效果图
商品数据
由于没有服务器,无法网络获取商品信息,故化繁从简,将需要的商品信息保存成xml文件,次项目将商品信息分为三个文件:标题文件,商品详细信息文件,商品图片文件。
商品标题文件
<resources><string-array name="title"><item>把酒言欢</item><item>爆炒干锅</item><item>海鲜派对</item><item>辣度自选</item><item>素菜最爱</item><item>旺季龙虾</item><item>无肉不欢</item><item>主食饱腹</item></string-array>
</resources>
商品详细文件信息
由于篇幅过长,就没有显示全部商品信息,每一个商品信息分为:商品名称,商品规格,商品价格三个小类
<?xml version="1.0" encoding="utf-8"?>
<resources><string-array name="content">
<!-- title:把酒言欢--><item>可乐</item><item>一瓶</item><item>3.00</item><item>雪花</item><item>一瓶</item><item>5.00</item><item>怡宝</item><item>一瓶</item><item>2.00</item></string-array></resources>
商品图片
由于篇幅过长,例举部分商品图片信息
<resources><integer-array name="icon"><item>@drawable/kele</item><item>@drawable/xuehua</item><item>@drawable/yibao</item></integer-array>
</resources>
添加商品数据
解析左侧标题数据
private void InitTitle(){String[] titleArray = getResources().getStringArray(R.array.title);for (int i = 0; i < titleArray.length; i++) {goodsTitle = new Goods.GoodsTitle(titleArray[i],i );titleList.add( goodsTitle );}}
解析右侧商品数据
private void InitContent(){TypedArray iconArray = getResources().obtainTypedArray(R.array.icon);int index = 0;String[] content = getResources().getStringArray(R.array.content);for (int i = 0; i < content.length ; i+=3) {//if (index < iconArray.length())/*把酒言欢*/if ( i < 3){goodsContent = new Goods.GoodsContent( iconArray.getResourceId(index++,0), content[i], content[i+1], Float.parseFloat(content[i+2]),R.drawable.icon_add,R.drawable.icon_sub,0,0 );}else if (i < 8){/*爆炒干锅*/goodsContent = new Goods.GoodsContent( iconArray.getResourceId(index++,0), content[i], content[i+1], Float.parseFloat(content[i+2]),R.drawable.icon_add,R.drawable.icon_sub,0,1 );}else if ( i < 12){/*海鲜派对*/goodsContent = new Goods.GoodsContent( iconArray.getResourceId(index++,0), content[i], content[i+1], Float.parseFloat(content[i+2]),R.drawable.icon_add,R.drawable.icon_sub,0,2 );}else if ( i < 16){/*辣度自选*/goodsContent = new Goods.GoodsContent( iconArray.getResourceId(index++,0), content[i], content[i+1], Float.parseFloat(content[i+2]),R.drawable.icon_add,R.drawable.icon_sub,0,3 );}else if ( i < 22){/*素菜最爱*/goodsContent = new Goods.GoodsContent( iconArray.getResourceId(index++,0), content[i], content[i+1], Float.parseFloat(content[i+2]),R.drawable.icon_add,R.drawable.icon_sub,0,4 );}else if ( i < 25){/*旺季龙虾*/goodsContent = new Goods.GoodsContent( iconArray.getResourceId(index++,0), content[i], content[i+1], Float.parseFloat(content[i+2]),R.drawable.icon_add,R.drawable.icon_sub,0,5 );}else if ( i < 28){/*无肉不欢*/goodsContent = new Goods.GoodsContent( iconArray.getResourceId(index++,0), content[i], content[i+1], Float.parseFloat(content[i+2]),R.drawable.icon_add,R.drawable.icon_sub,0,6 );}else/*主食饱腹*/goodsContent = new Goods.GoodsContent( iconArray.getResourceId(index++,0), content[i], content[i+1], Float.parseFloat(content[i+2]),R.drawable.icon_add,R.drawable.icon_sub,0,7 );contentList.add( goodsContent );}}
左侧标题与右侧商品双向绑定
我们在添加左侧标题的时候有一个参数,为每一个小类的标识。同样在添加商品数据的时候,根据商品所属小类进行了分类。
private void InitListener(){titleAdapter.setOnItemClickListener( new BaseQuickAdapter.OnItemClickListener() {@Overridepublic void onItemClick(BaseQuickAdapter adapter, View view, int position) {String[] titleArray = getResources().getStringArray(R.array.title);titleAdapter.setSelection( position );titleAdapter.notifyDataSetChanged();contentManager.scrollToPositionWithOffset( titleArray.length,0 );}} );mGoodsContent.addOnScrollListener( new RecyclerView.OnScrollListener() {@Overridepublic void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {super.onScrolled( recyclerView, dx, dy );int firstPosition = contentManager.findFirstVisibleItemPosition();goodsContent = contentList.get(firstPosition);int subId = goodsContent.getSubId();int pos = 0;for (int i = 0; i < titleList.size(); i++) {int id = titleList.get(i).getId();if ((subId == id)) {pos = i;}}titleAdapter.setSelection(pos);//mGoodsTitle.scrollToPosition( pos );titleAdapter.notifyDataSetChanged();}} );
加减按钮监听
对每一个商品的加减按钮进行点击事件监听
contentAdapter.setOnItemChildClickListener( new BaseQuickAdapter.OnItemChildClickListener() {@Overridepublic void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {double Price = contentList.get( position ).getPrice();switch (view.getId()){case R.id.Add:int numberAdd = contentList.get( position ).getNumber();numberAdd++;Monetary(Price);contentList.get( position ).setNumber( numberAdd );contentAdapter.notifyDataSetChanged();//Toast.makeText( FunctionActivity.this,"Add"+Price,Toast.LENGTH_SHORT ).show();break;case R.id.Sub:int numberSub = contentList.get( position ).getNumber();if (numberSub > 0 ){numberSub--;contentList.get( position ).setNumber( numberSub );LessMoney(Price);}else if (numberSub < 0){contentList.get( position ).setNumber( 0 );}contentAdapter.notifyDataSetChanged();//Toast.makeText( FunctionActivity.this,"Sub"+Price,Toast.LENGTH_SHORT ).show();break;}}} );
并且每次点击事件更新所选商品总额,并且对所选商品下限设定为15元,方才能进行支付。
private void Monetary(double Money){TotalMoney+=Money;money.setText( doubleToString(TotalMoney) );if (TotalMoney > 15){MoreThanStyle();}else {LessStyle();}}private void LessMoney(double Money){TotalMoney-=Money;if (TotalMoney < 15){LessStyle();money.setText( 0+"" );}else {MoreThanStyle();money.setText( doubleToString(TotalMoney) );}}
所选商品存入数据库
将所选的商品信息保存到数据库,方便对其进行存取
private void GotoSettlement(){String user = (String) sp.GetData(FunctionActivity.this,"UserName_login","");String pass = (String) sp.GetData(FunctionActivity.this,"PassWord_login","");for (int i = 0; i <contentList.size() ; i++) {int num = contentList.get( i ).getNumber();if (num > 0){/*插入数据为null*///Toast.makeText( context,""+num,Toast.LENGTH_SHORT ).show();goodsList = new GoodsList(user,pass, contentList.get( i ).getGoodsIMG(),contentList.get( i ).getContent(),contentList.get( i ).getPrice(),contentList.get( i ).getNumber(),R.drawable.icon_sub,R.drawable.icon_add );dao.Insert( goodsList );}}}
商品复选
此界面可以对所选商品进行复选,可以看见商品总额,并对其进行最后确认
效果图
取出商品数据
从数据库中将用户所选商品进行取出,并添加到此适配器中
private void InitData(){goodsLists = dao.QueryAll();for (int i = 0; i <goodsLists.size() ; i++) {goodsList = new GoodsList(goodsLists.get(i).getUserName(),goodsLists.get(i).getPassWord(), goodsLists.get( i ).getImg(),goodsLists.get( i ).getName(),goodsLists.get( i ).getPrice(),goodsLists.get( i ).getNumber(),goodsLists.get( i ).getSub(),goodsLists.get( i ).getAdd() );dataList.add( goodsList );}}
商品清空
将适配器中所有子项删除
goodsAllClear.setOnClickListener( new View.OnClickListener() {@Overridepublic void onClick(View v) {goodsListAdapter.removeAllItem( dataList );goodsTotalNumber.setText( 0+"" );goodsTotalPrice.setText( 0+"" );}} );
商品增删
private void Listener(){goodsListAdapter.setOnItemChildClickListener( new BaseQuickAdapter.OnItemChildClickListener() {@Overridepublic void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {int num = dataList.get( position ).getNumber();double price = dataList.get( position ).getPrice();switch (view.getId()){case R.id.goodsAdd:goodsListAdapter.addPrice(dataList,price,position,num);InitBarData();//Toast.makeText( GoodsListActivity.this,"Add"+num,Toast.LENGTH_SHORT ).show();break;case R.id.goodsSub:goodsListAdapter.subPrice(dataList,price,position,num);InitBarData();//Toast.makeText( GoodsListActivity.this,"Sub"+num,Toast.LENGTH_SHORT ).show();break;}}} );}
支付界面
效果图
密码匹配
此控件为自定义view,具体代码不展示,有兴趣的可以观看另外一篇文章,链接已挂
人脸支付与密码支付
payPassWord.setOnTextFinishListener(new PayPwdEditText.OnTextFinishListener() {@Overridepublic void onFinish(String str) {String Pay = "000000";if (str.equals( Pay )){toastFormat.setText("支付成功");toastFormat.show();startActivity(new Intent(PaymentActivity.this,PaySuccessActivity.class));}else {toastFormat.setText("密码错误");toastFormat.show();payPassWord.clearText();}}});
支付成功
效果图
SharedPreferences
利用SharedPreferences
完成数据传递,关于SharedPreferences封装可以观看另外一篇文章。
sp = SP.getInstance();money = (String) sp.GetData(PaySuccessActivity.this,"TotalPrice","");
历史数据图表化
此图标工具采用第三方APIhellocharts
效果图
数据初始化
从数据库取出购买商品数据
dao = new Dao(context);goodsLists = dao.QueryAll();Totalnum = goodsLists.size();
X轴
从数据库中取出商品名称,并作为X轴
数据
for (int i = 0; i <goodsLists.size() ; i++) {title.add(goodsLists.get(i).getName());}
Y轴
分为内外两层循环,外循环觉得总的小类,内循环决定一个小类有几根柱子;例如:用户购买了4个商品,分别显示商品的价格以及数量,则外循环为4,内循环为2;
for (int j = 0; j < Totalnum ; j++) {mPointValues = new ArrayList<>();for (int i = 0; i < single; i++) {mPointValues.add(new SubcolumnValue((goodsLists.get(j).getPrice()), color.get(j)));//值的大小、颜色//设置X轴的柱子所对应的属性名称(底部文字)axisXValues.add(new AxisValue(j).setLabel(title.get(j)));}Column column = new Column(mPointValues);ColumnChartValueFormatter chartValueFormatter = new SimpleColumnChartValueFormatter(2);column.setFormatter(chartValueFormatter);column.setHasLabelsOnlyForSelected(false);column.setHasLabels(true);//column.setValues(mPointValues);//将每个属性得列全部添加到List中//添加了7个大柱子Column,单个大柱子里面mPointValues大小为3(自行调整)columns.add(column);}
尾言
由于篇幅原因,并未将全部代码贴出