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

我正在尝试开发一个带有列表视图自定义适配器的测验应用程序,但是当我选择一个单选按钮并向下滚动时,我看到另一个单选按钮被自动选中,我使用了视图持有者,但我不知道如何解决这个问题。

我的自定义适配器的代码。


问题适配器


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();

        }


白衣染霜花
浏览 148回答 3
3回答

繁华开满天机

尝试在您的适配器中为每个单选按钮使用 if else 如下所示&nbsp;if(questionArrayList.get(position).getChoice1Selected())&nbsp;{&nbsp;&nbsp;viewHolder.choice1.setSelected(true);&nbsp; }else{&nbsp;&nbsp;viewHolder.choice1.setSelected(false);&nbsp;}&nbsp; &nbsp; &nbsp;&nbsp;

波斯汪

试试这个代码并根据您的代码进行更改,并将一个布尔字段带入问题 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) {&nbsp; &nbsp; this.onItemClick = onItemClick;}public interface OnItemClick {&nbsp; &nbsp; void getPosition(Question data); //pass any data to shared it.}public RecyclerViewAdpater(List<Question> mQuestionList, Context mContext) {&nbsp; &nbsp; this.mQuestionList = mQuestionList;&nbsp; &nbsp; this.mContext = mContext;}@Overridepublic ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {&nbsp; &nbsp; View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main, parent, false);&nbsp; &nbsp; return new ItemViewHolder(view);}@Overridepublic void onBindViewHolder(final ItemViewHolder holder, final int position) {&nbsp; &nbsp; // below code handle click event on recycler view item.&nbsp; &nbsp; final Question str=mQuestionList.get(position); //&nbsp; here your boject&nbsp; &nbsp; holder.textView.setText(str.getName());&nbsp; &nbsp; if (str.isSelected()){&nbsp; &nbsp; &nbsp; &nbsp; // hear set your radio button select or unselect.&nbsp; &nbsp; &nbsp; &nbsp; holder.textView.setVisibility(View.GONE);&nbsp; &nbsp; }&nbsp; &nbsp; holder.textView.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onItemClick.getPosition(str);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}@Overridepublic int getItemCount() {&nbsp; &nbsp; return mQuestionList.size();}public class ItemViewHolder extends RecyclerView.ViewHolder {&nbsp; &nbsp; TextView textView;&nbsp; &nbsp; public ItemViewHolder(View itemView) {&nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; textView=itemView.findViewById(R.id.textView);&nbsp; &nbsp; }}}在适配器定义之后,它意味着适配器绑定到回收器视图并且适配器不为空然后调用下面的代码..&nbsp; &nbsp; &nbsp; &nbsp; adpater.setOnItemClick(new RecyclerViewAdpater.OnItemClick() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void getPosition(Question data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // perform any operation here.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data.setSelected(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adpater.notifyDataSetChanged();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });如果您为您引用相同的鳕鱼,则只为 mange select 或 un select 单选按钮取一个布尔值。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java