Search code examples
androidlistviewdynamiclistadaptertogglebutton

Issue when i scroll in my listview it adds addtional ToggleButtons that i add with an Array


I have the Vraag Class that i use to make the different items in the List. That class has an Array of the Antwoord class. For each Antwoord i want a togglebutton that i added with the for loop. The problem now is that when i scroll in the list it adds additional togglebuttons to the list items. Here is my adapter code:

public class VraagListAdapter extends ArrayAdapter<Vraag>{
public Context context;
public List<Vraag> vragen;
ViewHolder holder;
int id = 0;

public VraagListAdapter(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) {     

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

    }

    holder.vraag.setText(vragen.get(position).getVraag());
    final int positievraag = position;

    holder.group.setOnCheckedChangeListener(ToggleListener);



    for(Antwoord a : vragen.get(position).getAntwoorden()){
        holder.toggle = new ToggleButton(context);
        holder.toggle.setText(a.getAntwoord());
        holder.toggle.setTextOn(a.getAntwoord());
        holder.toggle.setTextOff(a.getAntwoord());
        holder.toggle.setId(id);

        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());
                vragen.get(positievraag).setAntwoord((String)holder.toggle.getText());
            }
        });
        id++;

        holder.group.addView(holder.toggle);    
    }
    return convertView;
}

static class ViewHolder {
      TextView vraag;
      RadioGroup group;
      ToggleButton toggle;

      int position;
    }

static final RadioGroup.OnCheckedChangeListener ToggleListener = new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(final RadioGroup radioGroup, final int i) {
        for (int j = 0; j < radioGroup.getChildCount(); j++) {
            final ToggleButton view = (ToggleButton) radioGroup.getChildAt(j);
            view.setChecked(view.getId() == i);
        }
    }
};

}


Solution

  • I solved the problem by inserting convertView = null; at the beginning of the getView()

    The only problem i still got that ToggleButtons that where checked aren't checked anymore when I scroll down and up again! And how do i get all the checked Togglebuttons in my Activity?