[英]Add Radio Buttons of the same Radio Group in different table rows in 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);
}
}
您應該將按鈕添加到廣播組。
winnerGrp .addView(winnerBtn)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.