Issue
I want to activate disabled buttons called tglBtnLevel1, tglBtnLevel2, tglBtnLevel3... from 1 till the integer received with a method.
They only way I achieve it is using switch case, but I guess there must be a way using loops. I have tried a For loop, but I cannot find the way to include the counter (i) in the line "tglBtnLevel(i).setEnabled(true)".
I would thank you any hint or help. This is the beginning of the switch case I use to make it work, but only the first buttons, there are more:
private void checkEnabledLevels(){
switch (d.sendPlayerStats().getWeekTournamentLevel()){
case 1:
tglBtnLevel1.setEnabled(true);
break;
case 2:
tglBtnLevel1.setEnabled(true);
tglBtnLevel2.setEnabled(true);
break;
case 3:
glBtnLevel1.setEnabled(true);
tglBtnLevel2.setEnabled(true);
tglBtnLevel3.setEnabled(true);
break;
case 4:
glBtnLevel1.setEnabled(true);
tglBtnLevel2.setEnabled(true);
tglBtnLevel3.setEnabled(true);
tglBtnLevel4.setEnabled(true);
break;
Solution
One way of doing this could be to have all the buttons in an array like this:
Button[] buttonArray = {tglBtnLevel1, tglBtnLevel2, tglBtnLevel3, tglBtnLevel4}
And then simply iterator over them depending upon the tournament level you get:
int tournamentLevel = d.sendPlayerStats().getWeekTournamentLevel()
for (int i = 0; i < tournamentLevel; i++) {
buttonArray[i].setEnabled(true);
}
A slightly different but simple way of doing this could be like:
int tournamentLevel = d.sendPlayerStats().getWeekTournamentLevel()
tglBtnLevel1.setEnabled(tournamentLevel <= 1);
tglBtnLevel2.setEnabled(tournamentLevel <= 2);
tglBtnLevel3.setEnabled(tournamentLevel <= 3);
tglBtnLevel4.setEnabled(tournamentLevel <= 4);
Answered By - Pranav Jadhav
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.