我有一个Activity
,其中包含2个显示数字值的TextView
,其下方有RecyclerView
,RecyclerView
有TextView
个如果用户将SwitchCompat
中的SwitchCompat
中的ON
中的数字转换为TextView
,我想要的数值和RecyclerView
在TextView
之外的RecyclerView
,如果SwitchCompat
切换为OFF
,则会从外部TextView
中的数字中减去该数字。
我尝试将TextView
传递给RecyclerViewAdapter
并在适配器类中使用它,但错误发生在第一次正常但滚动随机数时要添加和减去TextView
,以下是我的代码:
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()));
}
}
答案 0 :(得分:0)
您应该尝试使用函数来更新textview值,就像每次检查一个视图一样:
@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();
}
}
}
}
您的更新功能应该如下(将对象替换为您的真实对象名称):
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);
}