为了账号安全,请及时绑定邮箱和手机立即绑定

带有列表视图的视图持有人单选按钮

带有列表视图的视图持有人单选按钮

白衣染霜花 2021-07-05 13:26:11
我正在尝试开发一个带有列表视图自定义适配器的测验应用程序,但是当我选择一个单选按钮并向下滚动时,我看到另一个单选按钮被自动选中,我使用了视图持有者,但我不知道如何解决这个问题。我的自定义适配器的代码。问题适配器public class QuestionAdapter2 extends ArrayAdapter<Question> {    Context context;    ArrayList<Question> questionArrayList;    public QuestionAdapter2(Context context, ArrayList<Question> questionArrayList) {        super(context, R.layout.question_listitem, questionArrayList);        this.context = context;        this.questionArrayList = questionArrayList;    }    static class ViewHolder {        protected TextView question;        protected RadioButton choice1;        protected RadioButton choice2;        protected RadioButton choice3;        protected RadioButton choice4;    }    @NonNull    @Override    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {        ViewHolder viewHolder = null;        if(convertView == null)        {            LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);            convertView=inflater.inflate(R.layout.question_listitem,parent,false);            viewHolder = new ViewHolder();            viewHolder.question = convertView.findViewById(R.id.question);            viewHolder.choice1 = convertView.findViewById(R.id.choice1);            viewHolder.choice2 = convertView.findViewById(R.id.choice2);            viewHolder.choice3 = convertView.findViewById(R.id.choice3);            viewHolder.choice4 = convertView.findViewById(R.id.choice4);            viewHolder.choice1.setSelected(false);            viewHolder.choice2.setSelected(false);            viewHolder.choice3.setSelected(false);            viewHolder.choice4.setSelected(false);            convertView.setTag(viewHolder);        }        else        {            viewHolder = (ViewHolder) convertView.getTag();        }
查看完整描述

3 回答

?
繁华开满天机

TA贡献1816条经验 获得超4个赞

尝试在您的适配器中为每个单选按钮使用 if else 如下所示


 if(questionArrayList.get(position).getChoice1Selected())

 {  

viewHolder.choice1.setSelected(true);

  }else{  

viewHolder.choice1.setSelected(false);

 }      


查看完整回答
反对 回复 2021-07-14
?
波斯汪

TA贡献1811条经验 获得超4个赞

试试这个代码并根据您的代码进行更改,并将一个布尔字段带入问题 pojo 类..


public class RecyclerViewAdpater extends RecyclerView.Adapter<RecyclerViewAdpater.ItemViewHolder> {

List<Question> mQuestionList = new ArrayList<>();// hear you can pass any pojo class object.

Context mContext;

OnItemClick onItemClick;


public void setOnItemClick(OnItemClick onItemClick) {

    this.onItemClick = onItemClick;

}


public interface OnItemClick {

    void getPosition(Question data); //pass any data to shared it.

}


public RecyclerViewAdpater(List<Question> mQuestionList, Context mContext) {

    this.mQuestionList = mQuestionList;

    this.mContext = mContext;

}


@Override

public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main, parent, false);

    return new ItemViewHolder(view);

}


@Override

public void onBindViewHolder(final ItemViewHolder holder, final int position) {

    // below code handle click event on recycler view item.

    final Question str=mQuestionList.get(position); //  here your boject

    holder.textView.setText(str.getName());

    if (str.isSelected()){

        // hear set your radio button select or unselect.

        holder.textView.setVisibility(View.GONE);

    }

    holder.textView.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View view) {

            onItemClick.getPosition(str);

        }

    });


}


@Override

public int getItemCount() {

    return mQuestionList.size();

}


public class ItemViewHolder extends RecyclerView.ViewHolder {

    TextView textView;


    public ItemViewHolder(View itemView) {

        super(itemView);

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

    }

}

}

在适配器定义之后,它意味着适配器绑定到回收器视图并且适配器不为空然后调用下面的代码..


        adpater.setOnItemClick(new RecyclerViewAdpater.OnItemClick() {

        @Override

        public void getPosition(Question data) {

            // perform any operation here.

            data.setSelected(true);

            adpater.notifyDataSetChanged();

        }

    });

如果您为您引用相同的鳕鱼,则只为 mange select 或 un select 单选按钮取一个布尔值。


查看完整回答
反对 回复 2021-07-14
  • 3 回答
  • 0 关注
  • 147 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信