Skip to content Skip to sidebar Skip to footer

Dynamically Generated Radio Button Changes Its Position While Scrolling

I have created dynamically Radio Buttons .And they are displayed quite well, but when I select and scroll it loses its position. I know the way of setting and getting the tag with

Solution 1:

I struggled with this issue last days, but doing R&N got the issue solved. you can do something like this.

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub// if(position==0)//Toast.makeText(context, ""+QuestionViewPageAdapter.mRadioGroupData, Toast.LENGTH_SHORT).show();

    final ViewHolder viewHolder;
    View row=convertView;

    radioButton=new RadioButton[Integer.parseInt(mNoOfCategoryGreading)]; LayoutInflater inflater =LayoutInflater.from(context);//(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.question_row, parent, false);

        viewHolder=new ViewHolder();

        viewHolder.tv_qContent=(TextView)row.findViewById(R.id.tv_question);
        viewHolder.rg=(RadioGroup)row.findViewById(R.id.rgrp);

        for(int i=0;i<Integer.parseInt(mNoOfCategoryGreading);i++)
        {
            radioButton[i]=new RadioButton(context);
            radioButton[i].setLayoutParams(new RadioGroup.LayoutParams(0, LayoutParams.WRAP_CONTENT,1f));
            //1f for layout_weight="1"
            radioButton[i].setId(i);
            radioButton[i].setText(""+(i+1));

            viewHolder.rg.addView(radioButton[i]);
        }
        row.setTag(viewHolder);


        for(int i=0;i<QuestionViewPageAdapter.mRadioGroupData.size();i++){
            int p=Integer.parseInt(""+QuestionViewPageAdapter.mRadioGroupData.get(i).get("position"));
            String id=QuestionViewPageAdapter.mRadioGroupData.get(i).get("Id");
            if(p!=-1 && id.equals(""+data.get(position).get(AppData.question_Set_category_QuestionID)))
            {//radioButton[p].setSelected(true);
                viewHolder.rg.check(p-1);}//Toast.makeText(context, "Yes "+p, Toast.LENGTH_LONG).show();}viewHolder.tv_qContent.setText((position+1)+". "+data.get(position).get(AppData.question_Set_category_QuestionContent));
    viewHolder.tv_qContent.setTypeface(typeface_muso_regular300);



    viewHolder.rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        publicvoidonCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub

            String s=data.get(position).get(AppData.question_Set_category_QuestionID);
            isAvailabel(s);


                    int radioButtonID = viewHolder.rg.getCheckedRadioButtonId();
                    View radioButton = viewHolder.rg.findViewById(radioButtonID);
                    int idx = viewHolder.rg.indexOfChild(radioButton);
                    HashMap<String, String> map=new HashMap<String, String>();
                    map.put("position", ""+(idx+1));
                    map.put("Id", ""+data.get(position).get(AppData.question_Set_category_QuestionID));
                    map.put("CategoryId", ""+mCategoryId);
                    QuestionViewPageAdapter.mRadioGroupData.add(map);
            }


    });





    return row;
}

privatevoidisAvailabel(String qId){

    for(int i=0;i<QuestionViewPageAdapter.mRadioGroupData.size();i++){
        if(qId.equals(QuestionViewPageAdapter.mRadioGroupData.get(i).get("Id"))){
            position=i;

            QuestionViewPageAdapter.mRadioGroupData.remove(i);
        }
    }

}

 classViewHolder {
     TextView tv_qContent ,tv_1,tv_2,tv_3;
        RadioGroup rg;
        RadioButton rBtn1,rBtn2,rBtn3,rBtn4,rBtn5;
 }

I couldn't remove the code that is not much important for you. you have to study the code and i think you will get the idea. Upvote if i solve your issue.

Post a Comment for "Dynamically Generated Radio Button Changes Its Position While Scrolling"