RecyclerView项目会影响RecyclerView外部的TextView的值

时间:2018-05-07 14:49:11

标签: java android android-recyclerview switchcompat

我有一个Activity,其中包含2个显示数字值的TextView,其下方有RecyclerViewRecyclerViewTextView个如果用户将SwitchCompat中的SwitchCompat中的ON中的数字转换为TextView,我想要的数值和RecyclerViewTextView之外的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()));

                    }
                }

1 个答案:

答案 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);

}