I'm trying to make a RadioGroup with active games where there is a message
"Your move!" beside those games where it is your move. So, I modified my
RadioGroup to have LinearLayouts with horizontal orientation as immediate
children, then nest the RadioButtons inside those along with a TextView that
has the appropriate message where needed. Unfortunately, when I put the
LinearLayout between the RadioButtons and the RadioGroup in the hierarchy,
the RadioGroup OnClickListener() stopped working.
So, my question is how to get the RadioGroup to listen while still
dynamically generating a message for those games where it's your move.
Here's the code that generates the custom RadioGroup (a class
ResumeRadioGroup that extends RadioGroup) and which is setting the tags
correctly:
private void init(Context context) {
db = new ChessDataBaseAdapter(context);
db.open();
playerCursor = db.getActiveGamePlayers();
numPlayers = playerCursor.getCount();
Log.d(TAG, numPlayers + " players found with active games");
final int playerNameCol =
playerCursor.getColumnIndex(PLAYER_NAME_COL);
final int playerIdCol =
playerCursor.getColumnIndex(PLAYER_ID_COL);
final int gameIdCol = playerCursor.getColumnIndex(GAME_ID_COL);
final int selfToMoveCol =
playerCursor.getColumnIndex(SELF_TO_MOVE_COL);
playerRadio = new RadioButton[numPlayers];
// new
LinearLayout container;
TextView moveText;
playerCursor.moveToFirst();
for (int i = 0; i < numPlayers; ++i) {
container = new LinearLayout(context);
playerRadio[i] = new RadioButton(context);
playerRadio[i].setText(playerCursor.getString(playerNameCol));
Log.d(TAG, "setTag() playerId: " +
playerCursor.getInt(playerIdCol) + ", gameId: " +
playerCursor.getInt(gameIdCol));
playerRadio[i].setTag(new
PlayerAndGame(playerCursor.getInt(playerIdCol),
playerCursor.getInt(gameIdCol)));
moveText = new TextView(context);
moveText.setGravity(Gravity.RIGHT);
if (playerCursor.getInt(selfToMoveCol) == 1) {
moveText.setText("Your move!");
moveText.setTextColor(Color.parseColor("#ffddad65"));
container.setBackgroundColor(Color.parseColor("#ff67656c"));
}
container.setPadding(5, 0, 5, 0);
container.setOrientation(HORIZONTAL);
container.addView(playerRadio[i],
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
container.addView(moveText, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
//*/
this.addView(container, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
playerCursor.moveToNext();
}
playerCursor.close();
db.close();
}
However, when I set up my listener for the RadioGroup as follows (in the
Activity that controls these views), nothing is happening when a button is
clicked:
private void init() {
selectedPlayer = -1;
selectedGame = -1;
continueButton = findViewById(R.id.continue_button);
gameRadioGroup = (RadioGroup) findViewById(R.id.resume_radio);
continueButton.setOnClickListener(this);
gameRadioGroup.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int
checkedId) {
selectedGame = ((PlayerAndGame)
findViewById(checkedId).getTag()).gameId();
selectedPlayer = ((PlayerAndGame)
findViewById(checkedId).getTag()).playerId();
}
});
}
That is, selectedPlayer and selectedGame just stay at -1 when a button is
clicked.
Any ideas how to fix this problem?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en