0
我有綁定到一個自定義一個ArrayAdapter類與含有一個TextView和組成3個單選按鈕一個RadioGroup中每行一個ListView。我正在使用ViewHolder模式來回收ListView中的視圖。
我已經寫了RadioGroup.setOnCheckedChangeListener捕捉單選按鈕「OnCheckedChange」事件。在事件處理程序中。我正在使用我的Model類跟蹤我的單選按鈕的狀態。
不過,我有,可以通過使用適配器GetView內支架模式單選按鈕支票簽發()method.Below是我的適配器類代碼 -的Android - ListView的與無線電按鈕
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使用當具有相同類型的問題與列表視圖中單選按鈕的鄉親。我無法讓我的代碼從任何上述解決方案中工作。
任何幫助是appreciated.Thanks。
@kris拉爾森 - 驚人的。這解決了我的問題。我明白了,在正確的位置設置「setOnCheckedChangeListener」爲空就行了。再次感謝。並且相信我,我嘗試了upvote,但是它只是被記錄下來,並且由於「低聲譽」而沒有公開顯示:)。 –