2

我有一个动态列表,我想在带有单选按钮的适配器视图中显示该列表。在一组。

我用了:

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>

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

4

1 回答 1

7
  <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]);           
  }       
于 2011-11-01T13:26:30.213 回答