如果onSwiped删除了购物车商品,如何更新(购物车总数)?

时间:2019-05-23 15:07:17

标签: android itemtouchhelper

我用哈希图中的产品和数量创建了一个购物车。如果通过onSwiped函数从购物车中删除了特定商品,如何更新哈希图?如何将哈希图传递到ItemTouchHelper中,以便确定与滑动(已删除)的viewHolder(适配器位置)关联的键值?

目前,对数量的更改或新项目将在哈希图中更新。我将此哈希图传递给指定的mapToArray函数,以更新总数。总而言之,我想分析哈希图,找到要删除的项目,将其对应的数量设置为零,然后将其发送到mapToArray。

private int mapToArray(HashMap selectionItemsHashMap) {
        selectionItemArrayList = null;
        selectionItemArrayList = new ArrayList<>();
        String selectionPrice = null;
        int amountTotal = 0;
        for (Object name: selectionItemsHashMap.keySet()){
            String key = (String) name;
            for (int j =0; j < menuItemArrayList.size(); j++ ) {
                String findName = menuItemArrayList.get(j).getMenuItemName();
                if (key.equals(findName)) {
                    selectionPrice = menuItemArrayList.get(j).getMenuItemPrice();
                }
            }
            int value = (int) selectionItemsHashMap.get(name);
            amountTotal += Integer.parseInt(selectionPrice)*value;
            String stringAmountTotal = Integer.toString(amountTotal);
            tvTotalAmount.setText(stringAmountTotal);
            SelectionItem selectionItem = new SelectionItem( key, value, selectionPrice);
            selectionItemArrayList.add(selectionItem);
        }
        return amountTotal;
    }

0 个答案:

没有答案