我想结合多订单项行以获取单行。像这样的{440059=1, Champagne,, 440055=5, Noodle,, 440057=2, Salad Soup,}当我登录时,它显示多于1行。

List<Cart> carts = db.getAllCarts();
for (Cart cart : carts) {
   Map<String, String> order = new HashMap<>();
   order.put(cart.getItemCode(), cart.getQty() + "," + cart.getDescription() + ",");

   Log.d("ord", String.valueOf(order));
}


日志记录

{440059=1,Champagne,}
{440055=5,Noodle,}
{440057=2,Salad Soup,}

最佳答案

将每个循环替换为以下循环

for(int i=0;i<carts.size();i++){
          java.util.Map<String, String> order = new java.util.HashMap<>();
          String desc = carts.get(i).getDescription();
          if(i != carts.size()-1)
              {
                desc = desc + ",";
              }
          order.put(carts.get(i).getItemCode(), carts.get(i).getQty() + "," + desc);
        }

关于android - 如何合并以获取HashMap中的单行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49993362/

10-11 00:08