2017-05-27 2 views
-2

3つのArrayListsがあります。 1つは、RecyclerView.Adapterが表示している製品のリストです。これはproductListと呼ばれます。 2番目のリストはcartItemsです。これは基本的にユーザーがproductListから選択したすべての製品を保持します。 3番目はカートの更新された値をすべて保持するupdatedCartです。私の目標は、updatedCartリストを使用してcartItemsとproductListを更新することです。私はproductIdsを比較することでこれをやっています。それらが等しい場合は、counterと呼ばれる値を更新し、そうでなければ削除します。しかし、私はIllegalStateExceptionを渡って来ています。私はこれについて私の論理についても100%確信していません。2つのArrayListsを繰り返し、新しいリストの値を使って元のものを更新する

これは、updatedCartから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を助けてください、あなたは私の唯一の希望です。

答えて

0

すべてのFisrt、第3のarraylistの目的は何ですか?ユーザーが更新すると(カートアイテムから追加/削除するもの)、そのカートアイテムのリストを更新します...なぜ別のarraylistを使用するのですか? 第2に、productlistが製品データ(ユーザーを表示するすべての製品)を更新する理由を含む配列のように、すべての単一ビットで完全なシナリオを記述しない限り、理解できない非常に複雑なロジックを作成しています。そのリスト? だから、できるだけシンプルにしてください。ユーザーがカートを更新するとすぐに製品リストとカートアイテムを更新しようとする別のロジックを作成します。

関連する問題