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()));
}
}