I have a recyclerview with a button in every row of products, i simply want to display the sum of all the product's cost live when they get clicked

Viewed 17

this is my RecyclerviewAdapter from where i m getting the Price of a product and Quantity of a product, by MyCallBack method in onBindViewHolder

public class RecyclerProductAdapter extends RecyclerView.Adapter<RecyclerProductAdapter.MyViewHolder> implements AdapterView.OnItemSelectedListener {

Context context;
ArrayList<Product> list;
MyCallBack myCallback;

public interface MyCallBack {
    void listenerMethod(int valueP, int value1, int value2);

}

public RecyclerProductAdapter(Context context, ArrayList<Product> list, MyCallBack myCallBack) {
    this.context = context;
    this.list = list;
    this.myCallback = myCallBack;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.row_product, parent, false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

    MyViewHolder vh = (MyViewHolder)holder;
    Product product = list.get(position);
    vh.xName.setText(product.getName());
    vh.xOldPrice.setText(product.getOldPrice());
    vh.xNewPrice.setText(product.getNewPrice());
    Glide.with(holder.xImage).load(product.getImage()).into(holder.xImage);

    ArrayAdapter<String> adapterKg = new ArrayAdapter<String>(context, R.layout.spinner_item_custom, product.getQuantity1());
    adapterKg.setDropDownViewResource(R.layout.spinner_dropdown_text);
    vh.xQuantityKg.setAdapter(adapterKg);
    vh.xQuantityKg.setOnItemSelectedListener(this);

    ArrayAdapter<String> adapterGms = new ArrayAdapter<String>(context, R.layout.spinner_item_custom, product.getQuantity2());
    adapterGms.setDropDownViewResource(R.layout.spinner_dropdown_text);
    vh.xQuantityGms.setAdapter(adapterGms);
    vh.xQuantityGms.setOnItemSelectedListener(this);

    LottieAnimationView addButton = vh.addButton;
    Spinner xQuantityKg = vh.xQuantityKg;
    Spinner xQuantityGms = vh.xQuantityGms;
    addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (vh.switchOn
                    &&
                    (xQuantityKg.getSelectedItem().toString().contains("▼"))
                    &&
                    (xQuantityGms.getSelectedItem().toString().contains("▼")
                            ||
                            xQuantityGms.getSelectedItem().toString().equals("empty0"))) {
                xQuantityKg.performClick();
                Toast toast = Toast.makeText(context, "पहले मात्रा चुनें", Toast.LENGTH_SHORT);
                toast.show();
            } else {
                if (vh.switchOn) {
                    addButton.setMinAndMaxProgress(0.0f, 0.66f);
                    addButton.playAnimation();
                    vh.switchOn = false;

                    if (xQuantityGms.getSelectedItem().toString().isEmpty()) {

                        int valueP = Integer.parseInt(vh.xNewPrice.getText().toString().replaceAll("[^0-9]", ""));
                        int value1 = Integer.parseInt(vh.xQuantityKg.getSelectedItem().toString().replaceAll("[^0-9]", ""));
                        int value2 = 0;
                        myCallback.listenerMethod(valueP, value1, value2);

                    } else {
                        int valueP = Integer.parseInt(vh.xNewPrice.getText().toString().replaceAll("[^0-9]", ""));
                        int value1 = Integer.parseInt(vh.xQuantityKg.getSelectedItem().toString().replaceAll("[^0-9]", ""));
                        int value2 = Integer.parseInt(vh.xQuantityGms.getSelectedItem().toString().replaceAll("[^0-9]", ""));
                        myCallback.listenerMethod(valueP, value1, value2);
                    }

                } else {
                    xQuantityKg.setSelection(0);
                    xQuantityGms.setSelection(0);
                    addButton.setMinAndMaxProgress(0.66f, 1.0f);
                    addButton.playAnimation();
                    vh.switchOn = true;
                }
            }
        }
    });
}

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

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if (parent.getSelectedItem().toString().equals("empty0")) {
        parent.setVisibility(View.GONE);
    } else {
        String txtKg = parent.getItemAtPosition(position).toString();
    }
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}

public class MyViewHolder extends RecyclerView.ViewHolder {

    boolean switchOn = true;
    LottieAnimationView addButton;
    TextView xName, xOldPrice, xNewPrice;
    ImageView xImage;
    Spinner xQuantityKg, xQuantityGms;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);

        xImage = itemView.findViewById(R.id.item_Product_Image);
        xName = itemView.findViewById(R.id.product_name);
        xNewPrice = itemView.findViewById(R.id.new_price);
        xOldPrice = itemView.findViewById(R.id.old_price);
        xQuantityKg = itemView.findViewById(R.id.quantity_spinner_kg);
        xQuantityGms = itemView.findViewById(R.id.quantity_spinner_gms);
        addButton = itemView.findViewById(R.id.btnAddItem);
    }
}
}

this is my Mainactivity where i m calling MyCallBack method, i want to display the updated sum of Price * Quantity for every product when its corresponding button get clicked

public class MainActivity extends AppCompatActivity implements RecyclerProductAdapter.MyCallBack {

RecyclerView recview;
RecyclerProductAdapter productAdapter;
FirebaseFirestore database;
ArrayList<Product> productList;
ProgressDialog progressDialog;
TextView total;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dashboard);

    progressDialog = new ProgressDialog(this);
    progressDialog.setCancelable(false);
    progressDialog.setMessage("प्रतीक्षा करें ...");
    progressDialog.show();

    recview = findViewById(R.id.idRecyclerView);
    ViewCompat.setNestedScrollingEnabled(recview, false);
    recview.setHasFixedSize(true);
    recview.setLayoutManager(new LinearLayoutManager(this));
total = findViewById(R.id.total_purchase);
}
@SuppressLint("SetTextI18n")
@Override
public void listenerMethod(int valueP, int value1, int value2) {

    int overTotal = 0;

    total.setVisibility(View.VISIBLE);
    final DecimalFormat df = new DecimalFormat("0.00");
    double value3 = value2 / 1000F;
    double value4 = value1 + value3;
    double valueF = valueP * value4;
    overTotal = (int) (overTotal + valueF);
    String valueFi = (df.format(overTotal));

    total.setText(valueFi);

}
}

this is real image of how things are working, the problem is, when i clicks a button its showing the total amount, but when i click another one the amount updates and shows new amount of this single row, i want to display the sum of my all selections, so when i add 3 products it should be like a sum of 3 products without refreshing the view.

this is real image of my project, i m new to java, please help!!!

0 Answers
Related