从recyclerview项目中将选定的RadioButton文本保存在模型类中

时间:2018-10-26 17:04:58

标签: java android android-recyclerview android-radiobutton

我从RecyclerView项中尝试将所选RadioButton的文本保存在模型类中。当我从第一项中选择RadioButton时,其文本将被适当保存。问题是,第8项中相同位置的RadioButton的文本也会自动保存。如果我从第二项中选择单选按钮,则也会自动保存第九项中的文本,依此类推。如何解决这个问题呢?

onBindViewHolder方法如下:

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

    ...

    holder.radioGroup.setTag(position);
    holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            int radioButtonID = group.getCheckedRadioButtonId();
            RadioButton radioButton = (RadioButton) group.findViewById(radioButtonID);
            int clickedPos = (Integer) group.getTag();
            models.get(clickedPos).setChecked(radioButtonID);

            if (radioButtonID > 0){
                models.get(clickedPos).setSelectedAns(radioButton.getText().toString());
            }

        }
    });
    holder.radioGroup.check(models.get(position).getChecked());
    Log.d("TAG", "At position " + position + " selected : " + models.get(position).getSelectedAns());

}

1 个答案:

答案 0 :(得分:2)

问题在于,尽管代码看上去表面上是正确的,但实际上发生的是,当您调用holder.radioGroup.check()时,它会触发onCheckedChanged()事件处理程序,就像用户启动它一样

由于视图被回收,因此位置0的视图将重新用于列表中的位置8。因此,在check()中对onBindViewHolder()的调用将呼叫onCheckedChanged(),而位置0的选中单选按钮仍处于选中状态(即checkedIdradioGroup. getCheckedRadioButtonId()将返回ID在位置0使用视图时选中的单选按钮的位置。

真正的症结所在

models.get(clickedPos).setChecked(radioButtonID);

考虑答案的第一段,您将意识到这将(错误地)使用在位置0使用此视图时检查的radioButtonID更新位置8的模型项。

解决此问题的一种方法是区分用户发起的更改和绑定发起的更改。例如,您可以通过向ViewHolder添加一个字段来指示视图当前是否绑定来完成此操作。

class ViewHolder extends RecyclerView.ViewHolder{
    TextView selectedAnswer;
    RadioGroup radioGroup;

    boolean isBinding;

    ViewHolder(View itemView) {
        super(itemView);

        radioGroup = itemView.findViewById(R.id.radioGroup);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int position = getAdapterPosition();

                RadioButton radioButton = (RadioButton) group.findViewById(checkedId);

                /* Only update the model when onCheckedChange() was initiated by the user clicking
                   a radio button, not when the adapter is binding the view. In that scenario, we
                   are only interested in passing information FROM the model TO the view. */
                if( !isBinding ) {
                    models.get(position).setChecked(checkedId);
                    models.get(position).setSelectedAns(radioButton != null ? radioButton.getText().toString() : "");
                }

                selectedAnswer.setText(  models.get(position).getSelectedAns() );
            }
        });

        ...
    }
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
    holder.isBinding = true;

    ...

    /* When calling check() here, we invoke onCheckedChanged(), which will
       update the textview that displays the selected answer - so no need to call
           holder.selectedAnswer.setText(  models.get(position).getSelectedAns() )
       from here */
    holder.radioGroup.check(models.get(position).getChecked());

    holder.isBinding = false;
}