将ArrayList值设置为for循环中的RadioButton

时间:2015-05-30 16:05:39

标签: android arraylist

我从服务器获取arrayList [5,8,13,18,19],我想在xml文件中创建RadioGroup。选择可解决的项目后,我会将所选项目放在arrayList中,然后在单击“确定”按钮后将查询发送到服务器。如何以编程方式创建RadioGroup

我试过这个,但我不知道如何设置RadioButton ArrayList值。我怎样才能让tp工作?

private void createRadioButton(final ArrayList<Integer> items) {

    final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
    final ArrayList<RadioButton> rb = new ArrayList<RadioButton>();
    final RadioGroup rg = new RadioGroup(this); // create the RadioGroup
    rg.setOrientation(RadioGroup.HORIZONTAL);// or RadioGroup.VERTICAL
    for (int i = 0; i < items.size(); i++) {
        items.get(i) = new RadioButton(this);
    }
}

1 个答案:

答案 0 :(得分:5)

试试这个

    final RadioGroup rg = new RadioGroup(this); // create the RadioGroup
    rg.setOrientation(RadioGroup.HORIZONTAL);// or RadioGroup.VERTICAL
    for (int i = 0; i < items.size(); i++) {
        RadioButton rb = new RadioButton(this);
        rb.setText(items.get(i)+"");
        rg.addView(rb);
    }

使用addView,您将动态创建的RadioButton添加到RadioGroup

相关问题