How to generate dynamic RadioButton with a radiongroup in adapterView?

Siten picture Siten · Nov 1, 2011 · Viewed 9.2k times · Source

I have a Dynamic list, and I want to show that list in adapter view with radio buttons. in one group.

I used:

public class Type2Adapter extends BaseAdapter{
    Context context;    
    LayoutInflater inflater;
    ArrayList<QueOption> oplist;
    String list1;
    int count;
    RadioGroup radiogroup;
    String dot,more;
    public Type2Adapter(Context context,ArrayList<QueOption> arrayList){
        this.context = context;
        inflater = LayoutInflater.from(context);
        oplist = arrayList;
        count = oplist.size();
        if(count>=1){
            count = oplist.size();
        }else{
            count = 1;
        }
    }


    @Override
    public int getCount() {     
        return count;       
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        
        ViewHolder holder;      
        if(convertView == null){    
            
            holder = new ViewHolder();  
            convertView = inflater.inflate(R.layout.type2_option, null);            
//          holder.nameList = (RadioButton)convertView.findViewById(R.id.optionRadio);          
            holder.layout = (LinearLayout)convertView.findViewById(R.id.type2);     
            radiogroup = (RadioGroup)convertView.findViewById(R.id.optionRadioGroup);
            convertView.setTag(holder);
        }
        else{
            holder = (ViewHolder) convertView.getTag();
        }           
            holder.layout.setBackgroundResource(R.drawable.list_back_rowcolor);
//          holder.nameList.setText(oplist.get(position).getOptionName());
            RadioButton newRadioButton = new RadioButton(context);
            newRadioButton.setId(position); 
            newRadioButton.setText(oplist.get(position).getOptionName());           
            radiogroup.addView(newRadioButton); 
            
        return convertView;
    }
    
    public class ViewHolder{
        
        private LinearLayout layout;
        private RadioButton nameList;
    }
    
}
 

XMl:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  
  <LinearLayout
    android:id="@+id/type2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
  
        <RadioGroup 
            android:id="@+id/optionRadioGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >  
            
            
        </RadioGroup>   
          
  </LinearLayout>  
  
</LinearLayout>

what should i do to solve my problem.

Answer

Hemant Menaria picture Hemant Menaria · Nov 1, 2011
  <RadioGroup 
            android:id="@+id/optionRadioGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" > 
   </RadioGroup>

//here i am adding 6 radiobutton .

rgroup=(RadioGroup)findViewById(R.id.optionRadioGroup);

for(int i=0; i<6; i++)
{
       rb[i]=new RadioButton(context);
       rb[i].setText("rdo"+i);
       rb[i].setId(i);
       rgroup.addView(rb[i]);           
  }