Reputation: 737
I've been searching for a while to help me with my question, but couldn't find any good tutorials. I started Android recently, so I'm not that good jet. My question is how I can convert my code in a way that I don't have to type everything. I was thinking maybe to use a counter. I want to know if there is a possibility that I only have to set the visibility of the seekbar and the button once. This is my code:
private void numberPlayers() {
Intent intent = getIntent();
String str = intent.getStringExtra("rbPlayers");
if (str.equals("rb2")) {
sbPlayer3.setVisibility(View.GONE);
bPlus3.setVisibility(View.GONE);
sbPlayer4.setVisibility(View.GONE);
bPlus4.setVisibility(View.GONE);
sbPlayer5.setVisibility(View.GONE);
bPlus5.setVisibility(View.GONE);
sbPlayer6.setVisibility(View.GONE);
bPlus6.setVisibility(View.GONE);
} else if (str.equals("rb3")) {
sbPlayer4.setVisibility(View.GONE);
bPlus4.setVisibility(View.GONE);
sbPlayer5.setVisibility(View.GONE);
bPlus5.setVisibility(View.GONE);
sbPlayer6.setVisibility(View.GONE);
bPlus6.setVisibility(View.GONE);
} else if (str.equals("rb4")) {
sbPlayer5.setVisibility(View.GONE);
bPlus5.setVisibility(View.GONE);
sbPlayer6.setVisibility(View.GONE);
bPlus6.setVisibility(View.GONE);
} else if (str.equals("rb5")) {
sbPlayer6.setVisibility(View.GONE);
bPlus6.setVisibility(View.GONE);
}
}
Thank you in advance!
Upvotes: 0
Views: 33
Reputation: 1893
If your layout elements are contained in each other then you can call the method on the parent one and it will call the same method on its children.
If your objects all inherit from the same base class you can put them in arrays and use a cycle to iterate through them and call the method.
Upvotes: 1
Reputation: 12887
This is not really related to Android, but I would suggest the following approach:
Hold all your variables (such as sbPlayer5
or bPlus6
) in an array - you can use ArrayList
to hold any type of object and have the array grow automatically. So you can access the correct object by a numeric index.
Pass rbPlayers
as an Integer
instead of String
.
Then iterate over your array using the numeric index (for
) and call setVisibility
inside the loop for all indexes above your maximum.
Upvotes: 1