利用GreenDao实现本地购物车(二)

接上文,已经实现了,GreenDao的导入,与创建开发需求所需要的数据类,以及自动生成相关的类。

接下来,就是数据库的操作。

首先,我们拿到数据库的操作对象,此处我选择的是在application中,可根据自己的需求自行选择。

/**
 * 获取本地数据库操作对象
 */
public CashierGoodsEntityDao getCashierGoodsEntityDao() {
    DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "cashier_goods_entity");
    Database db = helper.getWritableDb();
    DaoSession mDaoSession = new DaoMaster(db).newSession();
    CashierGoodsEntityDao cashierGoodsEntityDao = mDaoSession.getCashierGoodsEntityDao();
    return cashierGoodsEntityDao;
}

利用GreenDao实现本地购物车(二)_第1张图片

上述代码,兑换为自己的相应的类即可,拿到的 CashierGoodsEntityDao就可以进行一系列的数据的操作。

/**
 * 本地购物车的初始化
 */
public void setDbData() {
    cashierGoodsEntityDao = App.getInstance().getCashierGoodsEntityDao();
    cashierGoodsEntities = new ArrayList<>();
    cashierGoodsEntities.addAll(cashierGoodsEntityDao.loadAll());//获取所有的购物车数据
}
/**
 * 新增或者更新原有数据
 */
public void insertData() {
    entity = (CashierGoodsEntity) getIntent().getSerializableExtra("entity");
    if (entity == null || cashierGoodsEntities == null) {
        return;
    }
    if (cashierGoodsEntities.size() == 0) {
        cashierGoodsEntityDao.insert(entity);
    } else {
        for (CashierGoodsEntity cashEntity : cashierGoodsEntities) {
            if (cashEntity.getGoodsId().equals(entity.getGoodsId())) {
                int tempNumber = cashEntity.getNumber();
                cashEntity.setNumber(tempNumber + 1);
                cashierGoodsEntityDao.update(cashEntity);
                addNewData = false;
            }
        }
        if (addNewData) {
            cashierGoodsEntityDao.insert(entity);
        }
    }
    cashierGoodsEntities.clear();
    cashierGoodsEntities.addAll(cashierGoodsEntityDao.loadAll());//获取所有的购物车数据
    adapterCashier = new AdapterCashier(this, R.layout.item_cashier_buy_product_layout, cashierGoodsEntities,this::setNumChange);
    recyclerViewShop.setLayoutManager(new LinearLayoutManager(this));
    recyclerViewShop.setAdapter(adapterCashier);
    View headView = LayoutInflater.from(this).inflate(R.layout.cashier_head_layout_for_clear_layout, null);
    adapterCashier.addHeaderView(headView);
    TextView textViewDelete = headView.findViewById(R.id.tvForClear);
    textViewDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            clearAllShopData(true, "");

        }
    });

    adapterCashier.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
            CommDialog.newInstance(screen).setClickListen(new CommDialog.TwoSelDialog() {
                @Override
                public void leftClick() {
                }

                @Override
                public void rightClick() {
                    //确定
                    clearAllShopData(false, cashierGoodsEntities.get(position).getGoodsId());

                }
            }).setTitle("删除商品").setContent("是否删除商品").show();
            return false;
        }
    });
}

 

/**
 * 清除所有数据,结算或者点击清空数据时调用
 * 清空就清空当前所有数据,并且退出页面
 * 删除时 需要刷新数据
 */
public void clearAllShopData(boolean clearAll, String deleteId) {
    if (clearAll) {
        cashierGoodsEntityDao.deleteAll();
        finish();
        return;
    }

    if (StringUtil.isNull(deleteId)) {
        return;
    }

    for (CashierGoodsEntity cashEntity : cashierGoodsEntities) {
        if (cashEntity.getGoodsId().equals(deleteId)) {
            cashierGoodsEntityDao.delete(cashEntity);
        }
    }
    cashierGoodsEntities.clear();
    cashierGoodsEntities.addAll(cashierGoodsEntityDao.loadAll());//获取所有的购物车数据

    if (cashierGoodsEntities.size() == 0) {
        finish();
    }else {
        adapterCashier.setNewData(cashierGoodsEntities);
    }

}
@Override
public void setNumChange(int number, String goodsId) {

    for (int i = 0; i < cashierGoodsEntities.size(); i++) {
        if (cashierGoodsEntities.get(i).getGoodsId().equals(goodsId)) {
            if (number == 0) {
                clearAllShopData(false, goodsId);
                break;
            } else {
                cashierGoodsEntities.get(i).setNumber(number);
                cashierGoodsEntityDao.update(cashierGoodsEntities.get(i));
                cashierGoodsEntities.clear();
                cashierGoodsEntities.addAll(cashierGoodsEntityDao.loadAll());//获取所有的购物车数据
                adapterCashier.setNewData(cashierGoodsEntities);
                break;
            }

        }
    }

}

以下是购物车列表的 ,适配器中的代码:

public class AdapterCashier extends BaseQuickAdapter implements View.OnClickListener,
        EditDialogBuyProduct.dialogDoSure{
    private Context context;
    private int number;
    private String goodsId;
    private ItemChange listener;
    private int stock;//库存
    private int position;
   private TextView textViewNumber;

    public AdapterCashier(Context context, int layoutResId, @Nullable List data, ItemChange listener) {
        super(layoutResId, data);
        this.context = context;
        this.listener = listener;
    }

    @Override
    protected void convert(BaseViewHolder helper, CashierGoodsEntity item) {
        StringUtil.setCommontTextShow(helper.getView(R.id.tvItemProductName), item.getName());
        StringUtil.setCommontTextShow(helper.getView(R.id.tvItemProductStock), "当前库存为:" + item.getStockNumber());
        StringUtil.setTextViewShowProductPrice(item.getPrice(), helper.getView(R.id.tvItemProductPrice), true);
        GlideTools.setImageShowImgNormal(context, item.getImgUrl(), helper.getView(R.id.ivItemProductImg));
        StringUtil.setCommontTextShow(helper.getView(R.id.tvItemProductNumber), item.getNumber() + "");

        TextView textViewAdd = helper.getView(R.id.tvItemProductAdd);
        textViewNumber = helper.getView(R.id.tvItemProductNumber);
        TextView textViewMinus = helper.getView(R.id.tvItemProductMinus);
        textViewAdd.setTag(item);
        textViewMinus.setTag(item);
        textViewNumber.setTag(item);

        textViewAdd.setOnClickListener(this::onClick);
        textViewNumber.setOnClickListener(this::onClick);
        textViewMinus.setOnClickListener(this::onClick);
    }

    @Override
    public void onClick(View view) {
        if (view.getTag() == null) {
            App.showToast("tag为空");
            return;
        }
        CashierGoodsEntity entity = (CashierGoodsEntity) view.getTag();
        goodsId = entity.getGoodsId();
        stock = StringUtil.StringToInt(entity.getStockNumber());
        number = entity.getNumber();
        switch (view.getId()) {
            case R.id.tvItemProductAdd:

                if (number >= stock) {
                    App.showToast("库存数为:" + stock);
                    return;
                }
                number++;
                entity.setNumber(number);
                listener.setNumChange(number, goodsId);
                break;

            case R.id.tvItemProductNumber:

                EditDialogBuyProduct editDialog = new EditDialogBuyProduct(context, R.style.PXDialog,
                        this::whenPressSure, String.valueOf(number), goodsId);
                editDialog.show();
                break;

            case R.id.tvItemProductMinus:
                if (number >= 1) {
                    number--;
                    entity.setNumber(number);
                    listener.setNumChange(number, goodsId);
                }
                break;
        }

    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position, @NonNull List payloads) {
        super.onBindViewHolder(holder, position, payloads);
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else {
            textViewNumber.setText(payloads.get(0) + "");
        }

    }

    @Override
    public void whenPressSure(String number, String goodsId) {
        listener.setNumChange(StringUtil.StringToInt(number), goodsId);
    }

    public interface ItemChange {

        void setNumChange(int number, String goodsId);
    }
} 
  

其中,数据的输入是点击弹出一个框,实现数据的输入,此处不再赘述,我觉得不是什么问题,大家可以根据自己的需求,自行修改,此处只是将我的代码贴出来,仅供参考。

这是我这一两天的工作成果,写出来分享给大家,希望可以对大家有所帮助。

重点来了,走过路过不要错过,支持一下,打赏一下,十块八块都是爱。

利用GreenDao实现本地购物车(二)_第2张图片

 利用GreenDao实现本地购物车(二)_第3张图片

你可能感兴趣的:(GreenDao)