在android中的不同表行中添加相同Radio Group的Radio Button。

时间:2015-12-26 22:40:45

标签: java android

我在android中动态创建一个表,我希望在赢家列中有一个单选按钮。我创建了一个无线电组,并希望单选按钮成为该组的一部分,因此只能选择一个获胜者。但不知何故,我的所有单选按钮都同时启用。每个按钮都是独立的。我有下面的代码。

public void addData() {

    numOfPlayers = currentRound.getPlayers().size();
    row = new String[numOfPlayers];
    for (int i = 0; i < numOfPlayers; i++) {
        row[i] = currentRound.getPlayers().get(i).getPlayer().getName();
    }
    col = new String[]{"Winner", "Seen", "Less", "Points"}; // get from database

    int rowCount = row.length;

    RadioGroup winnerGrp = new RadioGroup(this);

    for (int i = 0; i <= rowCount; i++) {
        TableRow tableRow = new TableRow(this);
        RadioButton winnerBtn = new RadioButton(winnerGrp.getContext());
        tableRow.setId(i);
        // create tableRow
        for (int j = 0; j <= 4; j++) {
            //create textView
            int count = 1;
            TextView textView = new TextView(this);
            textView.setGravity(Gravity.CENTER);
            CheckBox checkBox = new CheckBox(this);
            EditText point = new EditText(this);
            if (i == 0 && j == 0) {
                textView.setText(" ");
                tableRow.addView(textView);
            } else if (i > 0 && j == 0) {
                textView.setText(row[i - 1]); // Player Header
                tableRow.addView(textView);
            } else if (i == 0 && j != 0) {
                textView.setText(col[j - 1]); //Game Header
                tableRow.addView(textView);
            } else if (i > 0 && j == 1) {
                winnerBtn.setText(""); // Is Winner
                winnerBtn.setId(count++);
                tableRow.addView(winnerBtn);
            } else if (i > 0 && j == 2) {
                checkBox.setText(""); // Is Seen
                checkBox.setId(count++);
                tableRow.addView(checkBox);
            } else if (i > 0 && j == 3) {
                checkBox.setText(""); // Is Less
                checkBox.setId(count++);
                tableRow.addView(checkBox);
            } else if (i > 0 && j == 4) {
                point.setInputType(100); // Points
                point.setId(count++);
                tableRow.addView(point);
            }
        }
        tl.addView(tableRow);
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

您应该将按钮添加到radiogroup。

winnerGrp .addView(winnerBtn)