2017-05-27 164 views
-2

我有三個ArrayList。一個是RecyclerView.Adapter顯示的產品列表。這一個叫做productList。第二個列表叫做cartItems,它基本上包含用戶從productList中選擇的所有產品。第三個稱爲updatedCart,它包含購物車的所有更新值。我的目標是使用updatedCart列表更新我的cartItems和productList。我通過比較productIds來做到這一點。如果它們相等,則我更新一個稱爲計數器的值,否則我將它們刪除。但是,我不斷遇到IllegalStateException。我對此的邏輯也不是100%肯定的。迭代兩個ArrayList,使用新列表中的值更新原始值

以下是我更新productCart中的productList和cartItems的方法。

public void updateCartItems(ArrayList<Product> updatedCart) { 

    // "productList" is a full list of products that the adapter displays 
    // "cartItems" is a list of items user adds to the cart 
    // "updatedCart" is a list of updated cart items (user can remove an item or increase the number of items he's purchasing) 
    // i want the original "cartItems" and "productList" to update it's values according to the "updatedCart" 
    if (updatedCart.size() == 0) { 
     Iterator<Product> productIterator = cartItems.iterator(); 
     while (productIterator.hasNext()) { 
      Product product = productIterator.next(); 
      productIterator.remove(); 
     } 
     for (Product listProduct : productList) { 
      listProduct.setCounter(0); 
     } 
    } else { 
     Iterator<Product> productIterator = cartItems.iterator(); 
     while (productIterator.hasNext()) { 
      Product product = productIterator.next(); 
      for (Product cartProduct : updatedCart) { 
       if (cartProduct.getProductId() != product.getProductId()) { 
        productIterator.remove(); 
       } else { 
        product.setCounter(cartProduct.getCounter()); 
       } 
      } 
     } 
     for (Product listProduct : productList) { 
      for (Product cartProduct : cartItems) { 
       if (listProduct.getProductId() == cartProduct.getProductId()) { 
        listProduct.setCounter(cartProduct.getCounter()); 
       } else { 
        listProduct.setCounter(0); 
       } 
      } 
     } 
    } 
    notifyDataSetChanged(); 
} 

這是我得到

05-28 03:25:31.769 23280-23280/com.innovationsquare.organicshuttle E/InputEventSender: Exception dispatching finished signal. 
05-28 03:25:31.769 23280-23280/com.innovationsquare.organicshuttle E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback 
05-28 03:25:31.771 23280-23280/com.innovationsquare.organicshuttle E/MessageQueue-JNI: java.lang.IllegalStateException 

出現在elseproductIterator.remove()行中的錯誤的錯誤。除了擺脫這個錯誤之外,我還想知道我的邏輯在這裏是否合理。 幫幫我stackoverflow,你是我唯一的希望。

回答

0

所有的問題,第三個數組列表的目的是什麼?如果用戶更新(從cartitems添加/刪除東西),然後更新該cartitem列表......爲什麼要使用另一個arraylist? 其次,您正在製作一個非常複雜的邏輯,直到您不會描述完整場景時纔會理解,例如productlist是包含產品數據的數組(包含所有要顯示的產品的用戶)爲什麼要更新那個清單?因此,儘可能保持簡單。在用戶更新購物車後儘快嘗試更新產品清單和cartitem的邏輯。

相關問題