【问题标题】:Find missing value from ArrayList in Java在 Java 中从 ArrayList 中查找缺失值
【发布时间】:2020-03-26 13:59:32
【问题描述】:

我遇到了 ArrayList 中的值丢失的问题。当我敬酒 listCartItemValues.toString() 时,根据现有数据,结果是正确的。但是当我在 Model DataItemCart 中设置 listCartItemValues 然后我举杯时,其中一个值丢失了,那就是 Starlight。

我的 API:

{
    "data": [
        {
            "cart_item_values": [
                {
                    "attribute_name": "color",
                    "attribute_value": "Starlight"
                },
                {
                    "attribute_name": "size",
                    "attribute_value": "XL"
                }
            ]
        },
        {
            "cart_item_values": [
                {
                    "attribute_name": "size",
                    "attribute_value": "10"
                }
            ]
        }
    ],
}

获取cart_item_values 的代码:

private List<CartItemValuesItem> listCartItemValues;

for (int a = 0; a < data.length(); a++) {

    JSONObject dataJSONObject = data.getJSONObject(a);

    final DataItemCart dataItemCart = new DataItemCart();
    final CartItemValuesItem cartItemValuesItem = new CartItemValuesItem();

    JSONArray arrayCartItemValues = dataJSONObject.optJSONArray("cart_item_values");

    for (int b = 0; b < arrayCartItemValues.length(); b++) {
        JSONObject cartItemJsonObject = arrayCartItemValues.optJSONObject(b);
        listCartItemValues = new ArrayList<>();

        cartItemValuesItem.setAttributeName(cartItemJsonObject.getString("attribute_name"));
        cartItemValuesItem.setAttributeValue(cartItemJsonObject.getString("attribute_value"));
        listCartItemValues.add(cartItemValuesItem);

        Toast.makeText(mContext, "value : "+ listCartItemValues.toString(), Toast.LENGTH_SHORT).show();
        // this list contain Starlight, XL, and 10
    }

    dataItemCart.setCartItemValues(listCartItemValues);

    Toast.makeText(mContext, "data : "+ dataItemCart.getCartItemValues.toString(), Toast.LENGTH_SHORT).show();
    // but this list contain XL and 10. Starlight is missing
}

如何从 ArrayList 中查找缺失值并将缺失值输入到dataItemCart.setCartItemValues 中?

【问题讨论】:

  • 为什么不把json转成map,然后进行操作呢?

标签: java android arraylist pojo


【解决方案1】:

"final DataItemCart dataItemCart = new DataItemCart();" 移出第一个 For 循环。

"listCartItemValues = new ArrayList();" 移出第二个 For 循环。

这是修改后的代码。

private List<CartItemValuesItem> listCartItemValues;
    final DataItemCart dataItemCart = new DataItemCart();
    for (int a = 0; a < data.length(); a++) {

        JSONObject dataJSONObject = data.getJSONObject(a);

        final CartItemValuesItem cartItemValuesItem = new CartItemValuesItem();

        JSONArray arrayCartItemValues = dataJSONObject.optJSONArray("cart_item_values");
        listCartItemValues = new ArrayList<>();

        for (int b = 0; b < arrayCartItemValues.length(); b++) {
            JSONObject cartItemJsonObject = arrayCartItemValues.optJSONObject(b);
            cartItemValuesItem.setAttributeName(cartItemJsonObject.getString("attribute_name"));
            cartItemValuesItem.setAttributeValue(cartItemJsonObject.getString("attribute_value"));
            listCartItemValues.add(cartItemValuesItem);

            Toast.makeText(mContext, "value : "+ listCartItemValues.toString(), Toast.LENGTH_SHORT).show();
            // this list contain Starlight, XL, and 10
        }

        dataItemCart.setCartItemValues(listCartItemValues);

        Toast.makeText(mContext, "data : "+ dataItemCart.getCartItemValues.toString(), Toast.LENGTH_SHORT).show();
        // but this list contain XL and 10. Starlight is missing
    }

【讨论】:

  • 感谢您的回答,但您的代码对我不起作用。我敬酒 dataItemCart.getCartItemValues 只得到 10 个。Starlight 和 XL 不见了
猜你喜欢
  • 2017-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-12
  • 2019-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多