猿问

安卓代码注释 能不能尽可能告诉详细一些!

 //获取当前用户所有历史订单数据

   public ArrayList<Order> getOrdersbyTableIndex(String username){

    //map表key是订单编号

    ArrayList<Order> orders = new ArrayList<Order>();

    HashMap<String, ArrayList<Dish>> ordersmap = new HashMap<String, ArrayList<Dish>>();

       ArrayList<Dish> dishes = new ArrayList<Dish>();

       if(TextUtils.isEmpty(username)){

        return orders;

       }

       SQLiteDatabase db = this.getWritableDatabase();

       Cursor cur = db.rawQuery("SELECT * FROM "+TABLE_ORDER, null);

       if (cur != null) {

           if (cur.moveToFirst()) {

               do {

                   int numColumn = cur.getColumnIndex(DISH_USERNAME);

                   String o_tableindex  = cur.getString(numColumn);

                   if(username.equals(o_tableindex)){

                       int COLID = cur.getColumnIndex(ORDER_ID);

                       String d_id = cur.getString(COLID);

                       

                       if(ordersmap.containsKey(d_id)){

                        dishes = ordersmap.get(d_id);

                       }else{

                        dishes = new ArrayList<Dish>();

                        ordersmap.put(d_id, dishes);

                       }

                       

                       int NUMPASS = cur.getColumnIndex(DISH_NAME);

                       String d_name = cur.getString(NUMPASS);


                       int COLPRICE = cur.getColumnIndex(DISH_PRICE);

                       int d_price = Integer.parseInt(cur.getString(COLPRICE).toString());



                       int COLPIC = cur.getColumnIndex(DISH_PIC);

                       int d_pic = Integer.parseInt(cur.getString(COLPIC).toString());



                       int COLNUM = cur.getColumnIndex(DISH_NUM);

                       int d_num = Integer.parseInt(cur.getString(COLNUM).toString());


                       int COLSHOP = cur.getColumnIndex(DISH_SHOP);

                       String c_shop = cur.getString(COLSHOP);


                       Dish dish = new Dish(d_name,d_price,d_pic,d_num, c_shop);

                       dishes.add(dish);

                   }

               }while( cur.moveToNext());

           }

       }

       db.close();

       for(String key: ordersmap.keySet()){

        Order order = new Order(key, ordersmap.get(key));

        orders.add(order);

       }

       System.out.print("获取订单数量:" + orders.size());

       return orders;

   }



cuni
浏览 1374回答 3
3回答

alionSSS

0 0我来拿分来了!    public ArrayList<Order> getOrdersbyTableIndex(String username){      //map表key是订单编号      ArrayList<Order> orders = new ArrayList<Order>();      HashMap<String, ArrayList<Dish>> ordersmap = new HashMap<String, ArrayList<Dish>>();         ArrayList<Dish> dishes = new ArrayList<Dish>();         if(TextUtils.isEmpty(username)){          return orders;         }         SQLiteDatabase db = this.getWritableDatabase();         Cursor cur = db.rawQuery("SELECT * FROM "+TABLE_ORDER, null);         if (cur != null) { //防止Cursor为null             if (cur.moveToFirst()) { //cur是一个指针,此处是说将指针移动到第一个位置。同时,cur.moveToFirst()的返回值是一个boolean,为ture证明有数据                 do {                     int numColumn = cur.getColumnIndex(DISH_USERNAME);//根据DISH_USERNAME拿到对应列的index                     String o_tableindex  = cur.getString(numColumn);//根据上面返回的numColumn即index,拿到对应的值                     if(username.equals(o_tableindex)){//如果username和o_tableindex相等                         int COLID = cur.getColumnIndex(ORDER_ID);                         String d_id = cur.getString(COLID);                                                  if(ordersmap.containsKey(d_id)){//如果ordersmap这个集合包含了d_id这个key                          dishes = ordersmap.get(d_id);//根据key拿到对应的value,此处value是一个ArrayList<Dish>集合                         }else{ //如果d_id不存在,就向ordersmap里面存                          dishes = new ArrayList<Dish>();                          ordersmap.put(d_id, dishes);                         }                         //下面代码方法和上面一样,我只写不同的                         int NUMPASS = cur.getColumnIndex(DISH_NAME);                         String d_name = cur.getString(NUMPASS);                         int COLPRICE = cur.getColumnIndex(DISH_PRICE);                         int d_price = Integer.parseInt(cur.getString(COLPRICE).toString());//把String转换为int(可能你数据库存的String类型的吧= =)                         int COLPIC = cur.getColumnIndex(DISH_PIC);                         int d_pic = Integer.parseInt(cur.getString(COLPIC).toString());                         int COLNUM = cur.getColumnIndex(DISH_NUM);                         int d_num = Integer.parseInt(cur.getString(COLNUM).toString());                         int COLSHOP = cur.getColumnIndex(DISH_SHOP);                         String c_shop = cur.getString(COLSHOP);                         Dish dish = new Dish(d_name,d_price,d_pic,d_num, c_shop);                         dishes.add(dish);                     }                 }while( cur.moveToNext());             }         }         db.close();//关闭数据库,数据库用完必须关闭         for(String key: ordersmap.keySet()){ //增强for循环,即foreach。此处遍历map,ketSet()是拿到对应map的key          Order order = new Order(key, ordersmap.get(key));//key和value          orders.add(order);         }         System.out.print("获取订单数量:" + orders.size());         return orders;     }

cuni

哈哈 给你给你 谢谢大神!项目过了!
随时随地看视频慕课网APP

相关分类

Android
我要回答