【问题标题】:Checkbox refreshing in false when I use notifyDataSetChanged();当我使用 notifyDataSetChanged() 时,复选框刷新为 false;
【发布时间】:2019-02-19 07:20:11
【问题描述】:

我的问题是标题上写的那个,在我开始使用 notifyDataSetChanged() 之前,我有一个带有复选框和单选按钮的 recyclerview(因为我需要更新 recyclerview)复选框和单选按钮已被选中,但现在检查不起作用。 (我认为它有效,但它立即变为错误)

我确信这一点,因为如果我评论 notifyDataSetChanged() 行,那么它仍然可以正常工作。

你们知道怎么解决吗?

适配器类的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();
}

}

提前谢谢你。

【问题讨论】:

  • 请添加适配器类的代码
  • @RavindraKushwaha 嗨,我让你有用于适配器类的 Java。

标签: java android checkbox android-recyclerview


【解决方案1】:

好的,伙计们,我只是为了解决这个问题,我从包含适配器的类中调用 notifyDataSetChanged()。这是因为 recyclerView 一直在刷新,并且复选框和单选按钮未打印为选中状态。

对我有用的解决方案是在 recyclerView 中调用 notifyDataSetChanged()。就像这样:

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

            notifyDataSetChanged();
        }
    });

【讨论】:

    猜你喜欢
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-07
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    相关资源
    最近更新 更多