简体   繁体   English

当我使用notifyDataSetChanged()时,复选框刷新为false;

[英]Checkbox refreshing in false when I use notifyDataSetChanged();

My problem is the one wrote on the title, I have a recyclerview with checkboxes and radiobuttons before I started to use the notifyDataSetChanged() (because I need to update the recyclerview) the checkboxes and radiobuttons were been checked, but now the checks don't work. 我的问题是写在标题上的问题,在我开始使用notifyDataSetChanged()之前,我有一个带有复选框和单选按钮的recyclerview(因为我需要更新recyclerview),所以已经选中了复选框和单选按钮,但是现在检查没有工作。 (I think that it work but it becomes false instantly) (我认为它可以正常工作,但立即变为虚假)

I am sure of this because if I comment the line of notifyDataSetChanged() then it still working as well. 我对此notifyDataSetChanged()把握,因为如果我注释notifyDataSetChanged()行,那么它仍然可以正常工作。

Do you guys know how to fix it? 你们知道如何解决吗?

Java for the adapter Class: 适配器类的Java:

public class OptionModesAdapter extends RecyclerView.Adapter<OptionModesAdapter.ViewHolder> {

private int fatherMode;
private int fatherId;
private Context context;
private AsyncAdapters asyncAdapters;
private ArrayList<Modes> modes;
private ArrayList<Modes> selected;
private ArrayList<IUrbanRadioButton> radiosSelected;

OptionModesAdapter(ArrayList<Modes> modes, Context context, int fatherMode, AsyncAdapters asyncAdapters) {
    this.modes = modes;
    this.context = context;
    this.fatherMode = fatherMode;
    this.selected = new ArrayList<>();
    this.asyncAdapters = asyncAdapters;
    this.radiosSelected = new ArrayList<>();
}

class ViewHolder extends RecyclerView.ViewHolder {
    private CheckBox cbOptionMode;
    private IUrbanRadioButton rbOptionMode;

    ViewHolder(View itemView) {
        super(itemView);
        cbOptionMode = itemView.findViewById(R.id.cb_option_mode);
        rbOptionMode = itemView.findViewById(R.id.rb_option_mode);
    }
}


@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View myView = LayoutInflater.from(parent.getContext()).inflate(R.layout.option_modes, parent, false);
    return new OptionModesAdapter.ViewHolder(myView);
}

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

    switch (fatherMode) {
        case 0:
            holder.rbOptionMode.setVisibility(View.VISIBLE);
            holder.cbOptionMode.setVisibility(View.GONE);
            holder.rbOptionMode.setText(modes.get(position).getTranslationName(context));
            holder.rbOptionMode.setParentName(String.valueOf(modes.get(position).getIdFather()));
            onClickRadioButton(holder.rbOptionMode, position);
            break;
        case 1:
            holder.cbOptionMode.setVisibility(View.VISIBLE);
            holder.rbOptionMode.setVisibility(View.GONE);
            holder.cbOptionMode.setText(modes.get(position).getTranslationName(context));
            onClickCheckBox(holder.cbOptionMode, position);
            break;
        default:
            Log.e(CustomConstants.EXCEPTION, "OptionModesAdapter: ln 72. Esto no es un modo admitido");
            break;
    }

}

private void onClickCheckBox(final CheckBox checkBox, final int position) {

    checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (selectedContainsCB(checkBox)) {
                removeOldCb(checkBox);
            } else {
                addNewCb(checkBox);
            }

            fatherId = modes.get(position).getIdFather();
            asyncAdapters.onInnerClicked(selected, false, fatherId);
        }
    });
}

private boolean selectedContainsCB(CheckBox checkBox) {
    Modes newMode = findModeByCheckBox(checkBox);

    return selected.contains(newMode);
}

private Modes findModeByCheckBox(CheckBox checkBox) {
    Modes modeToReturn = new Modes();

    for (Modes currentMode : modes) {
        if (currentMode.getTranslationName(context).equals(checkBox.getText().toString())) {
            modeToReturn = currentMode;
            break;
        }
    }

    return modeToReturn;
}

private void removeOldCb(CheckBox checkBox) {

    Modes modeToRemove = findModeByCheckBox(checkBox);
    checkBox.setChecked(false);
    selected.remove(modeToRemove);

}

private void addNewCb(CheckBox checkBox) {
    Modes modeToAdd = findModeByCheckBox(checkBox);
    checkBox.setChecked(true);
    selected.add(modeToAdd);
}

private void onClickRadioButton(final IUrbanRadioButton iUrbanRadioButton, final int position) {

    iUrbanRadioButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            settingFalseAllRadios();
            deletingAllModesRadios();
            addNewRb(iUrbanRadioButton);
            radiosSelected.add(iUrbanRadioButton);
            iUrbanRadioButton.setChecked(true);
            fatherId = modes.get(position).getIdFather();
            asyncAdapters.onInnerClicked(selected, true, fatherId);
        }
    });
}

private void settingFalseAllRadios() {
    for (IUrbanRadioButton iUrbanRadioButton : radiosSelected) {
        iUrbanRadioButton.setChecked(false);
    }

    radiosSelected.clear();
}

private void deletingAllModesRadios() {
    selected.clear();
}

private Modes findModeByRadioButton(IUrbanRadioButton iUrbanRadioButton) {

    Modes modeToReturn = new Modes();

    for (Modes currentMode : modes) {
        if (currentMode.getTranslationName(context).equals(iUrbanRadioButton.getText().toString())) {
            modeToReturn = currentMode;
            break;
        }
    }

    return modeToReturn;
}

private void addNewRb(IUrbanRadioButton iUrbanRadioButton) {
    Modes modeToAdd = findModeByRadioButton(iUrbanRadioButton);
    selected.add(modeToAdd);
}


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

} }

Thank you in advance. 先感谢您。

Ok guys, I just to solve that, I was calling notifyDataSetChanged() from the class which contain the Adapter. 好的,我只是想解决这个问题,我正在从包含Adapter的类中调用notifyDataSetChanged()。 This is because the recyclerView were refreshing all the times and the checkboxes and radiobuttons don't be printed as checked. 这是因为recyclerView一直在刷新,并且复选框和单选按钮未按选中状态打印。

The solution that works to me is calling the notifyDataSetChanged() inside the recyclerView. 对我有效的解决方案是在recyclerView内部调用notifyDataSetChanged()。 Just like this: 像这样:

checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //My code...

            notifyDataSetChanged();
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 notifyDataSetChanged(); 不刷新 - notifyDataSetChanged(); not refreshing NotifyDataSetChanged()是否不刷新列表? - NotifyDataSetChanged() not refreshing list? notifyDataSetChanged()未在gridview中刷新 - notifyDataSetChanged() not refreshing in gridview notifyDataSetChanged(); 不刷新ListView? - notifyDataSetChanged(); not refreshing ListView? 即使使用notifyDataSetChanged,我的ListFragment中的ArrayAdapter也不会刷新 - ArrayAdapter in my ListFragment won't refresh even when I use notifyDataSetChanged 在notifyDataSetChanged之后,查看寻呼机未刷新 - View pager is not refreshing after notifyDataSetChanged 当我对ComboBox使用Getter时,值为False - False value when I use a Getter for ComboBox 尽管我使用了Adapter.notifyDataSetChanged(),但在更改数据集后,Android ListView不会刷新 - Android ListView not refreshing after dataset changed, though I have used the Adapter.notifyDataSetChanged() 当我有两个recyclerview数据源,但是只有一个数据源被更改时,如何在recyclerview中使用notifyDataSetChanged() - How to use notifyDataSetChanged() in recyclerview, when I have two data sources for the recyclerview, but only one data source has been changed Struts 2 checkbox:当fieldValue只能为true或false时,value属性有什么用? - Struts 2 checkbox : What's the use of the value attribute when the fieldValue can only be true or false?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM