我有三个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,你是我唯一的希望。