如何组合在HashMap中获取单行?

时间:2018-04-24 04:19:05

标签: android hashmap

我想组合多个订单项行以获得单行。像这样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)); } 当我记录时,它显示超过1行。

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

日志

Util.isImageSize = function (file, callback) {
    var fileUpload = file;
    var reader = new FileReader();
    reader.readAsDataURL(fileUpload.files[0]);
    reader.onload = function (e) {
        var image = new Image();
        image.src = e.target.result;
        image.onload = function () {
            var height = this.height;
            callback(height);
        };
    }
}

2 个答案:

答案 0 :(得分:1)

使用以下一个

替换每个循环
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);
        }

答案 1 :(得分:0)

您可以改为使用TextUtils.join:

List<Cart> carts = db.getAllCarts();
String result;
for (Cart cart : carts) {
  Map<String, String> order = new HashMap<>();
  result = TextUtils.join(", ", carts );
}
Log.d("ord", result);