0

I have an Activity which contains 2 TextView shows a numerical value and there is a RecyclerView below them, the RecyclerView has a TextView with a numerical value and a SwitchCompat, I want if the user turned the SwitchCompat to ON the number in the TextView inside the RecyclerView to be added to the number at the TextView outside the RecyclerView and if the SwitchCompat is switched to OFF the number is subtracted from the number in the outside TextView.

I tried to pass the TextView to the RecyclerViewAdapter and use it in the Adapter Class but the error happens that it works fine for the first time but when scrolling random numbers to be added and subtracted from the TextView ,, the following is my code :

AdapterClass.java

    @Override
    public void onBindViewHolder(@NonNull final ExpensesListAdapterViewHolder holder, int position) {
...
holder.switchComp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    eList.get(holder.getAdapterPosition()).setmFlag(isChecked);

                if (isChecked) {
                    if (TextUtils.isEmpty(eList.get(holder.getAdapterPosition()).getmExpenseName())) {
                        // switch is on + Increase Budget

                        totalBudget.setText(String.valueOf(Double.parseDouble(totalBudget.getText().toString())+eList.get(holder.getAdapterPosition()).getmAmount()));
                        totalRemaining.setText(String.valueOf(Double.parseDouble(totalRemaining.getText().toString())+eList.get(holder.getAdapterPosition()).getmAmount()));

                    } else if (TextUtils.isEmpty(eList.get(holder.getAdapterPosition()).getmI_B_Name())) {
                        // switch is on + expense

                        totalExpense.setText(String.valueOf(Double.parseDouble(totalExpense.getText().toString())+eList.get(holder.getAdapterPosition()).getmAmount()));
                        totalRemaining.setText(String.valueOf(Double.parseDouble(totalRemaining.getText().toString())-eList.get(holder.getAdapterPosition()).getmAmount()));

                    }

                } else {

                    if (TextUtils.isEmpty(eList.get(holder.getAdapterPosition()).getmExpenseName())) {
                        // switch is off + increase budget

                        totalBudget.setText(String.valueOf(Double.parseDouble(totalBudget.getText().toString())-eList.get(holder.getAdapterPosition()).getmAmount()));
                        totalRemaining.setText(String.valueOf(Double.parseDouble(totalRemaining.getText().toString())-eList.get(holder.getAdapterPosition()).getmAmount()));


                    } else if (TextUtils.isEmpty(eList.get(holder.getAdapterPosition()).getmI_B_Name())) {
                        // switch is off + expense

                        totalExpense.setText(String.valueOf(Double.parseDouble(totalExpense.getText().toString())-eList.get(holder.getAdapterPosition()).getmAmount()));
                       totalRemaining.setText(String.valueOf(Double.parseDouble(totalRemaining.getText().toString())+eList.get(holder.getAdapterPosition()).getmAmount()));

                    }
                }

1 Answers1

0

You should try using a function to update the textview values like everytime one view is checked:

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


    holder.switchComp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            eList.get(holder.getAdapterPosition()).setmFlag(isChecked);

            updateTotalValues();

        }
    }
    }
}

your update function should look like (replace object to your real objects name):

public void updateTotalValues(){
Double sumRemaining = 0d;
Double sumBudget = 0d;
Double sumExpense = 0d;

for(eList e : List<Object>){
    boolean isChecked = e.getmFlag;

    if (isChecked) {
        if (TextUtils.isEmpty(e.getmExpenseName())) {
            // switch is on + Increase Budget
            sumBudget+= e.getmAmount();
            sumRemaining+= e.getmAmount();

        } else if (TextUtils.isEmpty(e.getmI_B_Name())) {
            // switch is on + expense
            sumExpense+=e.getmAmount();
            sumRemaining+= e.getmAmount();
        }

    } else {

        if (TextUtils.isEmpty(e.getmExpenseName())) {
            // switch is off + increase budget
            sumBudget+= e.getmAmount();
            sumRemaining-= e.getmAmount();

        } else if (TextUtils.isEmpty(e.getmI_B_Name())) {
            // switch is off + expense
            sumExpense-=e.getmAmount();
            sumRemaining+= e.getmAmount();

        }
}

totalBudget.setText(sumBudget);
totalRemaining.setText(sumRemaining);
totalExpense.setText(sumExpense);

}