android – 如何在adapterView中使用radiongroup生成动态RadioButton?

我有一个动态列表..

我想用适配器按钮在适配器视图中显示该列表.在一个组中.

我用了:

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>

我该怎么做才能解决我的问题.

解决方法:

  <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]);           
  }       
上一篇:android-吐司不从列表中的选定项目生成文本


下一篇:android-Horizo​​ntalScrollView和适配器