Skip to content
Advertisement

sum value from any list item in Recyclerview Adapter

This is my CartAdapter code, and in onBindViewHolder it get data from Getter and Setter (Get Cart). My purpose is to sum all of the product prices from all data that shows up in cart fragment.

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.ViewHolder> {

private List<GetCart> getProducts;
private LayoutInflater layoutInflater;
private Context context;
ApiServices apiServices;
private int sum = 0;
private static boolean add = true;

public CartAdapter(Context context) {
    this.getProducts = new ArrayList<>();
    this.layoutInflater = LayoutInflater.from(context);
    this.context = context;
}

public void setProductsList(List<GetCart> getProductList){
    this.getProducts.clear();
    this.getProducts.addAll(getProductList);
    notifyDataSetChanged();
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = layoutInflater
            .inflate(R.layout.list_keranjang, parent, false);
    ViewHolder holder = new ViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(CartAdapter.ViewHolder holder, int position) {
    holder.tv_namaproduct.setText(getProducts.get(position).getNamaItem());
    holder.tv_productid.setText(getProducts.get(position).getItemId());
    holder.tv_hargatotal.setText(getProducts.get(position).getHarga());
    holder.et_jumlah.setText(getProducts.get(position).getJumlah());

    int price = Integer.parseInt(getProducts.get(position).getHarga());
    int count = getItemCount();

    for (int i = 0; i < count; i++){
        int tsum = 0;
        tsum = tsum + price;

        Log.d("total pay : ", String.valueOf(tsum));
    }
}

@Override
public int getItemCount() {
    return getProducts.size();
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    ImageView iv_delete;
    TextView tv_namaproduct, tv_productid, tv_hargatotal;
    EditText et_jumlah;

    public ViewHolder(View itemView) {
        super(itemView);

        apiServices = ApiUtils.getApiServices();
        iv_delete = itemView.findViewById(R.id.iv_delete);
        tv_namaproduct = itemView.findViewById(R.id.tv_namaproduct);
        tv_productid = itemView.findViewById(R.id.tv_productid);
        tv_hargatotal = itemView.findViewById(R.id.tv_hargatotal);
        et_jumlah = itemView.findViewById(R.id.et_jumlah);

        iv_delete.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int position = getAdapterPosition();

        String detailcartID = getProducts.get(position).getDetailCartId();

        if (detailcartID != null){
            deleteitem(detailcartID);
        }else {
            Toast.makeText(context, "Item tidak dikenali",
                    Toast.LENGTH_SHORT).show();
        }

    }
}

private void deleteitem(String detailcartID) {
    apiServices.deleteItem(detailcartID)
            .enqueue(new Callback<DeleteCartItemResponse>() {
                @Override
                public void onResponse(Call<DeleteCartItemResponse> call, Response<DeleteCartItemResponse> response) {
                    Intent a = new Intent(context, MainMember.class);
                    context.startActivity(a);
                    Toast.makeText(context, "Item berhasil dihapus",
                            Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(Call<DeleteCartItemResponse> call, Throwable t) {
                    Toast.makeText(context, "Item gagal dihapus",
                            Toast.LENGTH_SHORT).show();
                }
            });
}
}

And this is my Log result from get total price :

07-23 23:20:27.740 19703-19703/com.box.billy.billybox D/total pay :: 350
07-23 23:20:27.740 19703-19703/com.box.billy.billybox D/total pay :: 350
07-23 23:20:27.757 19703-19703/com.box.billy.billybox D/total pay :: 8000
07-23 23:20:27.757 19703-19703/com.box.billy.billybox D/total pay :: 8000

and not even get total price, but just get each price separatedly

thankyou Nigel Brown, I’ve tried your answer and got this on my Log:

D/total pay :: 35000
D/total pay :: 70000
D/total pay :: 105000
D/total pay :: 3500
D/total pay :: 7000
D/total pay :: 10500
D/total pay :: 800000
D/total pay :: 1600000
D/total pay :: 2400000

this is my fragment that provides all item has been added to the cart list :

This is my Screenshoot from Cart List Fragment

Advertisement

Answer

the problem is you declared the tsum in the for loop so it is getting set to zero each time it should be:

int tsum = 0;
for (int i = 0; i < count; i++){      
    tsum = tsum + price;
}
Log.d("total pay : ", String.valueOf(tsum));

edit:

Based on your update from your question you should not be trying to make the total within the adapter. The onBindViewHolder will be called over and over as the views are created. You need to generate the views with your list and then once the list is made add up, use the totals from the items in the list and display in the view.

You will need to save each quantity for the items in getCart and then outside the adapter use the loop to go though each item and multiply the quantity by the price and add it to the total, do it for all items and you should be good.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement