我正在尝试开发一个带有列表视图自定义适配器的测验应用程序,但是当我选择一个单选按钮并向下滚动时,我看到另一个单选按钮被自动选中,我使用了视图持有者,但我不知道如何解决这个问题。
我的自定义适配器的代码。
问题适配器
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();
}
繁华开满天机
波斯汪
相关分类