messed up dynamic radio buttons in ArrayAdapter

275 views Asked by At

when i run my app, i get much more radiobuttons than i need. It seems the radiobuttons repeat themselves in the same group. I don't really understand what is is going on. Here is my custom ArrayAdapter. I would like to know the problem here

public class QuestionsListAdapter extends ArrayAdapter<QuestionProperties> {


List<QuestionProperties> list;



Context test;



public QuestionsListAdapter(Context context,  int resource, List<QuestionProperties> list2) {
    super(context,resource,list2);
    test = context;
    list =list2;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view;

    final RadioButton[] rB;
     RadioHolder  holder = new RadioHolder();
    view= convertView;

    LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
            RadioGroup.LayoutParams.WRAP_CONTENT,
            RadioGroup.LayoutParams.WRAP_CONTENT);

        if(view == null)
        {
            LayoutInflater inflator = ((Activity) test).getLayoutInflater();
           view = inflator.inflate(R.layout.question_list_row, null);


            holder.questionTV = (TextView) view.findViewById(R.id.qTextView);
            holder.radiogroup = (RadioGroup) view.findViewById(R.id.radio_group);
            view.setTag(holder);

        }
        else{
            //view = convertView;
            holder = (RadioHolder) view.getTag();
            }



        holder.questionTV.setText(String.valueOf(list.get(position).getQuestionNo())+"."+" " + list.get(position).getQuestion());

        rB=new RadioButton[list.get(position).possibleAns.length];

        for(int count = 0; count<(list.get(position).possibleAns.length);count++)
        {
             rB[count]= new RadioButton(test);

            rB[count].setId(count);
            rB[count].setText(list.get(position).possibleAns[count]);
            layoutParams.weight=1.0f;
            layoutParams.setMargins(15, 0, 5, 10);
            rB[count].setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {

                    String a = String.valueOf(v.getId());
                    Toast.makeText(QActivity.context, "Radio Button "+ a,Toast.LENGTH_SHORT).show();    
                  }
            });
            holder.radiogroup.addView(rB[count],layoutParams);  

        }
        return view;
}



  static class RadioHolder {
        protected TextView questionTV;
        protected RadioGroup radiogroup;

    }
1

There are 1 answers

0
Nana Yaw On BEST ANSWER

Finally after some hacks i solved it! i removed all the radio buttons in the else clause. The solution..

public class QuestionsListAdapter extends ArrayAdapter<QuestionProperties> {


List<QuestionProperties> list;

RadioButton rB;

Context test;

RadioHolder  holder;

String chkBtn;

LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
        RadioGroup.LayoutParams.WRAP_CONTENT,
        RadioGroup.LayoutParams.WRAP_CONTENT);




public QuestionsListAdapter(Context context,  int resource, List<QuestionProperties> list2) {
    super(context,resource,list2);
    test = context;
    list =list2;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view;


     holder = new RadioHolder();
    view= convertView;
    Log.v("ConvertView", String.valueOf(position));



        if(view == null)
        {
            LayoutInflater inflator = ((Activity) test).getLayoutInflater();
           view = inflator.inflate(R.layout.question_list_row, parent,false);

           holder.questionTV = (TextView) view.findViewById(R.id.qTextView);
            holder.radiogroup = (RadioGroup) view.findViewById(R.id.radio_group);



            //holder.radiogroup.check(list.get(position).getSelectedAns());

            view.setTag(holder);
             //((RadioHolder) view.getTag()).radiogroup.setTag(list.get(position));
            Log.v("holder setTag", String.valueOf(position));



        }
        else{
            view = convertView;
             holder = (RadioHolder)view.getTag();
            //((RadioHolder)view.getTag()).radiogroup.getTag();

            holder.radiogroup.removeAllViews();


        }



        holder.questionTV.setText(String.valueOf(list.get(position).getQuestionNo())+"."+" " + list.get(position).getQuestion());
        configureRadioButtons(position);
        chkBtn = String.valueOf(list.get(position).getSelectedAns());
        holder.radiogroup.check(Integer.valueOf(chkBtn));


        return view;
}



  static class RadioHolder {
        protected TextView questionTV;
        protected RadioGroup radiogroup;

    }


    public void configureRadioButtons(int pos){

        final int position = pos;
        //rB=new RadioButton(test);

        for(int count = 0; count<(list.get(position).possibleAns.length);count++)
        {
             rB= new RadioButton(test);

            rB.setId(count);
            rB.setText(list.get(position).possibleAns[count]);
            layoutParams.weight=1.0f;
            layoutParams.setMargins(15, 0, 5, 10);
            holder.radiogroup.addView(rB,layoutParams); 

            rB.setOnClickListener(new OnClickListener(){

                        @Override
                        public void onClick(View v) {
                            String a = String.valueOf(v.getId());
                            list.get(position).setSelectedAns(v.getId());
                        chkBtn =    String.valueOf(list.get(position).getSelectedAns());

                            Toast.makeText(QActivity.context, "Radio Button "+ a,Toast.LENGTH_SHORT).show();    

                        }           
            });

            rB.setOnCheckedChangeListener(new OnCheckedChangeListener(){

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {


                }

            });

            holder.radiogroup.clearCheck();
            Log.v("rB added to radiogroup", String.valueOf(position));

        }

    }