Android 菜商品列表展示

Android 菜商品列表展示_第1张图片

如上图所示

右边也是一个数据列表,但它里面包含图片、文本框等等,并且每列都会有与之对应的餐类。

Android 部分

获取右侧商品listview的控件
private ListView rightListView = (ListView) view.findViewById(R.id.menu_lvmenu);
通过Http请求服务器,设置方法名参数到后台查询成功后返回json字符串,JSONObject获取json对象data,再通过gson1.fromJson获取数据到mGoodsList集合中,再执行initData()方法,初始化数据。initData方法中配置了ListView的适配器,主要是显示列表数据。
String urlgoodsAll=ServiceUrls.getMenuItemsMethodUrl("selectGoodsAll")+ "?catalog_food_id=" + 0;
OkHttpTool.httpPost(urlgoodsAll, null, new OkHttpTool.ResponseCallback() {
    @Override
    public void onResponse(boolean isSuccess, int responseCode, String response, Exception exception) {
        mActivityContext.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String strText = "网络环境不佳,请稍后再试";
                if (isSuccess && responseCode == 200 && Tools.isNotNull(response)) {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        String strData =  jsonObject.getString("data");
                        JSONArray jsonArray=new JSONArray(strData);
                        Gson gson1=new Gson();
                        mGoodsList= gson1.fromJson(strData, new TypeToken>() {}.getType());
                        initData();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
});
private void initData() {
    rightAdapter = new RightAdapter(mActivityContext,mGoodsList);
    //setAdapter 适配器就是用来显示列表项
    rightListView.setAdapter(rightAdapter); 
}
RightAdapter适配器实现SectionIndexer接口, getCount获取list集合中的条目个数,getItem根据索引获取对应的内容,getItemId获取索引,getView获取当前条目需要展示的布局效果,viewProductHolder通过容器来存储,优化找控件的内存,判断如果没有可复用那就获取控件绑定到view上,最后对控件的数据进行设置,再通过发送请求到后端查询每一列的图片。
class RightAdapter extends BaseAdapter implements SectionIndexer {
     private List list = null;
     private Context mContext;
     public RightAdapter(Context mContext, List mGoodsList) {
         this.mContext = mContext;
         this.list = mGoodsList;
     }
    @Override
     public int getCount() { 
         return this.list.size();
     }
     @Override
     public Object getItem(int position) { 
         return list.get(position);
     }
     @Override
     public long getItemId(int position) { 
         return position;
     }
     @Override
     public View getView(int position, View convertView, ViewGroup parent) { 
         final ViewProductHolder viewProductHolder;
         final GoodsBean mGoods = list.get(position);
         if (convertView == null) {
             convertView = LayoutInflater.from(mContext).inflate(R.layout.product_item, null);
             TextView tvTag = (TextView) convertView.findViewById(R.id.cate_name);
             TextView tvTitle = (TextView) convertView.findViewById(R.id.product_name_view);
             ImageView productImage = (ImageView) convertView.findViewById(R.id.product_imageView);
             TextView countView = (TextView) convertView.findViewById(R.id.add_count_view);
             TextView tvPrice = (TextView) convertView.findViewById(R.id.product_price_view);
             ImageView removeProductImage = (ImageView) convertView.findViewById(R.id.delete_product_view);
             ImageView addProductImage = (ImageView) convertView.findViewById(R.id.add_product_view);
             viewProductHolder = new ViewProductHolder(tvTag,tvTitle,productImage, countView,tvPrice, removeProductImage, addProductImage);

             convertView.setTag(viewProductHolder);
         } else {
             viewProductHolder = (ViewProductHolder) convertView.getTag();
         }
         //如果有该类型,则隐藏
         int section = getSectionForPosition(position);
         if (position == getPositionForSection(section)) {
             viewProductHolder.tvTag.setVisibility(View.VISIBLE);
             viewProductHolder.tvTag.setText(mGoods.getCatalogFoodname());
         } else {
             viewProductHolder.tvTag.setVisibility(View.GONE);
         }
         viewProductHolder.tvTitle.setText(mGoods.getGoodsName());
         viewProductHolder.tvPrice.setText("¥" + mGoods.getGoodsPrice());

         int salecount = UIHelper.saleCount(getActivity(), mGoods.getGoodsId(),businessId);
         if (salecount > 0) {
             viewProductHolder.countView.setText(salecount + "");
             viewProductHolder.countView.setVisibility(View.VISIBLE);
             viewProductHolder.removeProductImage.setVisibility(View.VISIBLE);
         } else {
             viewProductHolder.countView.setText(0 + "");
             viewProductHolder.countView.setVisibility(View.GONE);
             viewProductHolder.removeProductImage.setVisibility(View.GONE);
         }
         /**
          * 查询获取商品的图片
          * 请求
          */
         List photo= Collections.singletonList(mGoods.getGoodsPicture());
         for (int i=0;i

SSM 后端部分

通过@ RequestMapping注解将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上。 方法传入参数商品种类的id,根据种类id查询这个类下面有那几个商品,最后放到List集合中,json返回数据。在这是有两种情况,一种是全部查询,另一种是条件查询。

@ResponseBody
	@RequestMapping(value = "/selectGoodsAll", produces = "application/json;charset=UTF-8")
	public Object selectGoodsAll(int catalog_food_id) {
		JsonReturn jsonReturn = new JsonReturn();
		if(catalog_food_id>0) {
			List listGoodsAll = this.appMenuItemsService.selectlistGoodsByKind(catalog_food_id);
			if(listGoodsAll!=null) {
				jsonReturn.setCode(200);
				jsonReturn.setData(listGoodsAll);
			}else {
				jsonReturn.setCode(500);
				jsonReturn.setText("商品加载失败");
			}
		}else {
			List listGoodsByKind=this.appMenuItemsService.selectlistGoodsAll();
			if(listGoodsByKind!=null) {
				jsonReturn.setCode(200);
				jsonReturn.setData(listGoodsByKind);
			}else {
				jsonReturn.setCode(500);
				jsonReturn.setText("商品加载失败");
			}
		}
		return gson.toJson(jsonReturn);

	}
获取商品图片,判断参数是否为空,设置照片所在的本地路径(定义一行AppSeting类public static final String UPLOAD_GOODS_TYPE_PIC_DIR = "D:\\课程\\安卓移动开发\\点餐吧\\商品图片";),再判断你所对应的路径文件是否存在,设置Header,设置为图片类型
通过Android传过来的图片名称,到文件中查询出来再返回。

@RequestMapping(value="/getGoodsPicture")
	public ResponseEntity getGoodsPicture(String pictureName) throws IOException {
		// 判断是否为空
		if (Tools.isNotNull(pictureName)) {
			// 文件 照片的路经 ,名称
			File file = new File(AppSeting.UPLOAD_GOODS_TYPE_PIC_DIR, pictureName);
			// 判断文件是否存在
			if (file.exists()) {
				// 设置Header
				HttpHeaders headers = new HttpHeaders();
				// 设置为图片类型
				headers.setContentType(MediaType.IMAGE_JPEG);
				headers.setContentDispositionFormData("attachment", pictureName);
				// 返回文件相关数据
				return new ResponseEntity(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
			}
		}
		return null;
	}

 

你可能感兴趣的:(Android 菜商品列表展示)