2013-10-08 80 views
0

我有一个片段,我试图添加一个动态的以编程方式单选按钮到收音机组。单选按钮已添加,但文字未设置。设置单选按钮文本

这是我的代码:

private View myFragmentView; 
private Context context; 
private RadioGroup radioGrup; 

private ArrayList<Language> allLanguages; 

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    allLanguages=MyAsyncTask.getAllLanguages(); 
    myFragmentView = inflater 
      .inflate(R.layout.language_view, container, false); 
    context = this.getActivity().getApplicationContext(); 

    radioGrup = (RadioGroup)myFragmentView.findViewById(R.id.allLanguages); 
    addRadioButton(); 

    return myFragmentView; 
} 

@SuppressLint({ "ResourceAsColor", "NewApi" }) 
private void addRadioButton(){ 
    for(Language language:allLanguages){ 
     RadioButton newRadioButton = new RadioButton(context); 
     newRadioButton = new RadioButton(context); 
     newRadioButton.setText("something"); 
     newRadioButton.setTextColor(android.R.color.black); 
     newRadioButton.setLayoutParams 
      (new LayoutParams 
        (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     radioGrup.addView(newRadioButton); 
    } 
} 

有人能帮助我吗?

+0

为什么你复制newRadioButton的创造? –

回答

0

修改代码以

private void addRadioButton(){ 
    for(Language language:allLanguages){ 
     RadioButton newRadioButton = new RadioButton(context); 
     newRadioButton = new RadioButton(context); 
     newRadioButton.setLayoutParams 
      (new LayoutParams 
        (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

     newRadioButton.setText("something"); 
     newRadioButton.setTextColor(android.R.color.black); 
     radioGrup.addView(newRadioButton); 
    } 
}