我有三個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
出現在else
塊productIterator.remove()
行中的錯誤的錯誤。除了擺脫這個錯誤之外,我還想知道我的邏輯在這裏是否合理。 幫幫我stackoverflow,你是我唯一的希望。