ArrayからRadioGroupにトグルボタンを動的に追加します。追加は正常に機能し、すべての異なるリスト項目は正常に機能しますが、スクロールするとチェック済みのステータスが元に戻ります。どうすればこれを修正できますか?
ここに私のListAdapterクラスがあります:
public class BezoekverslagDetailsListAdapter extends ArrayAdapter<Vraag>{
public Context context;
public List<Vraag> vragen;
ViewHolder holder;;
public BezoekverslagDetailsListAdapter(Activity context, List<Vraag> vragen){
super(context, R.layout.vraag_item, vragen);
this.context = context;
this.vragen = vragen;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = null;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.vraag_item, null, false);
holder = new ViewHolder();
holder.vraag = (TextView)convertView.findViewById(R.id.tv_vraag);
holder.group = (RadioGroup)convertView.findViewById(R.id.toggleGroup);
holder.position = position;
holder.toggle = new ToggleButton(context);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
final Vraag vraag = vragen.get(position);
holder.vraag.setText(vraag.getVraag());
holder.group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group, int checkedId){
for (int j = 0; j < group.getChildCount(); j++) {
final ToggleButton view = (ToggleButton) group.getChildAt(j);
view.setChecked(view.getId() == checkedId);
vraag.getAntwoorden().get(view.getId()).setSelected(view.getId() == checkedId);
}
}
});
for(final Antwoord a : vraag.getAntwoorden()){
holder.toggle = new ToggleButton(context);
holder.toggle.setText(a.getAntwoord());
holder.toggle.setTextOn(a.getAntwoord());
holder.toggle.setTextOff(a.getAntwoord());
holder.toggle.setId(a.getId());
holder.toggle.setChecked(a.getSelected());
if(a.getVisible() == false){
holder.toggle.setVisibility(ToggleButton.INVISIBLE);
}
holder.toggle.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
// your click actions go here
((RadioGroup)view.getParent()).check(view.getId());
}
});
holder.group.addView(holder.toggle);
}
return convertView;
}
static class ViewHolder {
TextView vraag;
RadioGroup group;
ToggleButton toggle;
int position;
}
}