Android - 带单选按钮的ListView

时间:2016-07-27 19:52:21

标签: android listview android-radiogroup

我有一个ListView绑定到一个自定义ArrayAdapter类,每行包含一个TextView和一个由3个RadioButtons组成的RadioGroup。我正在使用ViewHolder模式来回收ListView中的视图 我编写了一个RadioGroup.setOnCheckedChangeListener来捕获RadioButton“OnCheckedChange”事件。在事件处理程序内部。我正在使用我的Model类跟踪我的Radio Buttons的状态 但是,我遇到了使用Adapters GetView()方法中的holder模式设置单选按钮检查的问题.Below是我的Adapter类代码 -

    public class QuestionsAdapter extends ArrayAdapter<QuestionAudit>{
Context context;
private List<QuestionAudit> questions;

public QuestionsAdapter(Context context, int resourceId, List<QuestionAudit> questions){
    super(context,resourceId,questions);
    this.context = context;
    this.questions = questions;
}

/*private view holder class*/
private class ViewHolder {
    TextView tv_question;
    RadioGroup rg_answer_options;
    RadioButton rb_answer_yes;
    RadioButton rb_answer_no;
    RadioButton rb_answer_na;

}

@Override
public int getCount() {
    return questions.size();
}

@Override
public QuestionAudit getItem(int position) {
    return questions.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    View rowView = convertView;
    QuestionAudit question = (QuestionAudit) getItem(position);

    if (rowView == null) {
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        rowView = mInflater.inflate(R.layout.list_item_patrol_question, null);
        holder = new ViewHolder();
        holder.tv_question = (TextView) rowView.findViewById(R.id.tv_question);

        holder.rg_answer_options = (RadioGroup) rowView.findViewById(R.id.rg_answer_options);

        holder.rb_answer_yes = (RadioButton) rowView.findViewById(R.id.rb_answer_yes);
        holder.rb_answer_no = (RadioButton) rowView.findViewById(R.id.rb_answer_no);
        holder.rb_answer_na = (RadioButton) rowView.findViewById(R.id.rb_answer_na);

        rowView.setTag(holder);

    } else {
        holder = (ViewHolder) rowView.getTag();

    }

    holder.tv_question.setText(question.getQuestion());

    holder.rg_answer_options.setTag(position);

    //set onClickListener
    holder.rg_answer_options.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
            //Integer position = (Integer) radioGroup.getTag();

            switch (checkedId) {
                case R.id.rb_answer_yes:
                    questions.get(position).setIsAnswerYes(true);
                    CommonHelperFuncs.displayToast(getContext(), "position: " + Integer.valueOf(position) + "  yes");
                    break;
                case R.id.rb_answer_no:
                    questions.get(position).setIsAnswerNo(true);
                    CommonHelperFuncs.displayToast(getContext(), "position: " + Integer.valueOf(position) + "  no");
                    break;
                case R.id.rb_answer_na:
                    questions.get(position).setIsAnswerNa(true);
                    CommonHelperFuncs.displayToast(getContext(), "position: " + Integer.valueOf(position) + "  na");
                    break;
            }
        }
    });

    if(question.getIsAnswerYes())
        holder.rb_answer_yes.setChecked(true);
    else
        holder.rb_answer_yes.setChecked(false);

    if(question.getIsAnswerNo())
        holder.rb_answer_no.setChecked(true);
    else
        holder.rb_answer_no.setChecked(false);

    if(question.getIsAnswerNa())
        holder.rb_answer_na.setChecked(true);
    else
        holder.rb_answer_na.setChecked(false);
    return rowView;
}

}

为了确保我的其余代码,我尝试不使用GetView()中的ViewHolder模式进行测试,它工作得很好。
我做了我的研究,当与ViewHolder一起使用时,看到很多人在列表视图中遇到与RadioButtons相同的问题。我无法从任何提到的解决方案中获得我的代码 感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

        // clear the listener while you're checking radio buttons
        holder.rg_answer_options.setOnCheckedChangeListener(null);

        if (question.getIsAnswerYes()) {
            holder.rg_answer_options.check(R.id.rb_answer_yes);
        } else if (question.getIsAnswerNo()) {
            holder.rg_answer_options.check(R.id.rb_answer_no);
        } else if (question.getIsAnswerNa()) {
            holder.rg_answer_options.check(R.id.rb_answer_na);
        } else {
            holder.rg_answer_options.clearCheck();
        }

        // now set your listener
        holder.rg_answer_options.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                .
                .
                .