Java 更改数据后如何计算价格?

Java 更改数据后如何计算价格?,java,android,Java,Android,我正在尝试创建一个电子商务应用程序。目前,我正在创建添加到购物车片段。因此,我成功地将项目添加到购物车并显示在CartFragment中。但问题是,当删除产品时,如果必须选中notifyDataSetChanged()和all复选框,则价格会上涨。我想当一个产品被删除,然后所有的价格总和计算 删除产品之前: 删除产品后: 我的适配器代码: int total_all_item; if (price_after_discount != 0) { product

我正在尝试创建一个电子商务应用程序。目前,我正在创建添加到购物车片段。因此,我成功地将项目添加到购物车并显示在CartFragment中。但问题是,当删除产品时,如果必须选中notifyDataSetChanged()和all复选框,则价格会上涨。我想当一个产品被删除,然后所有的价格总和计算

删除产品之前:

删除产品后:

我的适配器代码:

    int total_all_item;

    if (price_after_discount != 0) {
        productCart.setTotalPrice(price_after_discount);
        String price = (String.format("Rp%,d", productList.get(position).getPriceAfterDiscount())).replace(",", ".");
        productList.get(position).setFinalPrice(productList.get(position).getPriceAfterDiscount());
        productList.get(position).setTotalAllProduct(productList.get(position).getPriceAfterDiscount());
        holder.tvPrice.setText(price);
    } else {
        productCart.setTotalPrice(price_reguler);
        String price = (String.format("Rp%,d", productList.get(position).getPriceRegular())).replace(",", ".");
        productList.get(position).setFinalPrice(productList.get(position).getPriceRegular());
        productList.get(position).setTotalAllProduct(productList.get(position).getPriceRegular());
        holder.tvPrice.setText(price);
    }

    holder.ibDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            deleteCartList(context, qty, product_id, notes, dataItemCart, productCart, position);
        }
    });

    holder.checkBox.setChecked(true);
    if (holder.checkBox.isChecked()) {
        int currentQty = dataItemCartList.get(position).getQuantity();

        if (currentQty > 0) {
            total_all_item += productList.get(position).getFinalPrice() * currentQty;
            listener.onItemCheck(dataItemCart, productCart, dataItemCheckoutList, productCheckoutList);
            listener.setTotalPrice(total_all_item);

            dataItemCheckout.setId(dataItemCartList.get(position).getId());
            dataItemCheckout.setCurrentPrice(dataItemCartList.get(position).getCurrentPrice());
            dataItemCheckout.setLastPrice(dataItemCartList.get(position).getLastPrice());
            dataItemCheckout.setQuantity(dataItemCartList.get(position).getQuantity());
            dataItemCheckout.setNotes(dataItemCartList.get(position).getNotes());

            dataItemCheckoutList.add(dataItemCheckout);

            productCheckout.setId(productList.get(position).getId());
            productCheckout.setImage(productList.get(position).getImage());
            productCheckout.setName(productList.get(position).getName());
            productCheckout.setPriceAfterDiscount(productList.get(position).getPriceAfterDiscount());
            productCheckout.setPriceRegular(productList.get(position).getPriceRegular());

            productCheckoutList.add(productCheckout);

            listener.sendData(dataItemCheckout, productCheckout, dataItemCart, productCart, dataItemCheckoutList, productCheckoutList, dataItemCartList, productList, holder.checkBox.isChecked());
        }
    }
用于删除产品的代码:

private void deleteCartList(final Context context, int qty, int product_id, String notes, final DataItemCart dataItemCart, final Product productCart, final int position) {
    ResponseDeleteCart model = new ResponseDeleteCart(
            qty,
            notes,
            product_id
    );

    Call<JsonObject> categoryCall = baseApiService.deleteCartlist(model);
    categoryCall.enqueue(new Callback<JsonObject>() {
        @Override
        public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {

            if (response.isSuccessful()) {
                try {
                    notifyDataSetChanged();
                    Log.e(TAG, "onResponse :" + response.body().toString());

                    JSONObject jsonObject = new JSONObject(response.body().toString());

                    if (jsonObject.getString("status").equals("success")) {
                        String message = "Item berhasil dihapus";

                        dataItemCartList.remove(dataItemCart);
                        productList.remove(productCart);
                        notifyItemRemoved(position);

                        notifyDataSetChanged();

                        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
                    } else {
                        String message = "Item gagal dihapus";

                        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    JSONObject jObjError = new JSONObject(response.errorBody().string());
                    Toast.makeText(context, "message : " + jObjError.getString("message"), Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        }

        @Override
        public void onFailure(Call<JsonObject> call, Throwable t) {
            Toast.makeText(context, "Maaf, terjadi kesalahan!", Toast.LENGTH_SHORT).show();
            Log.e(TAG, "onFailure :" + t.toString());
        }
    });
}
private void deleteCartList(最终上下文上下文、整数数量、整数产品标识、字符串注释、最终DataItemCart DataItemCart、最终产品productCart、最终整数位置){
ResponseDeleteCart模型=新ResponseDeleteCart(
数量,
笔记,
产品标识
);
Call categoryCall=baseApiService.deleteCartlist(模型);
categoryCall.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
试一试{
notifyDataSetChanged();
Log.e(标记“onResponse:+response.body().toString());
JSONObject=newJSONObject(response.body().toString());
if(jsonObject.getString(“status”).equals(“success”)){
String message=“Item berhasil dihapus”;
dataItemCartList.remove(dataItemCart);
productList.remove(productCart);
已移除(位置)的项目;
notifyDataSetChanged();
Toast.makeText(上下文、消息、Toast.LENGTH_SHORT).show();
}否则{
字符串消息=“项目gagal dihapus”;
Toast.makeText(上下文、消息、Toast.LENGTH_SHORT).show();
}
}捕获(JSONException e){
e、 printStackTrace();
}
}否则{
试一试{
JSONObject jObjError=新的JSONObject(response.errorBody().string());
Toast.makeText(上下文,“message:+jObjError.getString(“message”),Toast.LENGTH\u LONG.show();
}捕获(例外e){
Toast.makeText(上下文,例如getMessage(),Toast.LENGTH_LONG.show();
}
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(上下文,“Maaf,terjadi kesalahan!”,Toast.LENGTH_SHORT.show();
Log.e(标记“onFailure:+t.toString());
}
});
}

您的适配器代码是否为您的
onBindViewHolder
方法显示?@COYG正确,来自我的适配器的代码是
onBindViewHolder
方法中的代码