Reputation:
I am developing an android application for a friend of mine that is a football trainer.
He asked me to create an application that is more of a table to count the stats of the players. The image below should clear things up:
As you can see, every time I press the upper left button I want to add a new player two a new row with a decrement button a counter and an increment button below each stat column.
I can currently do that but each player has 10 different column counters. So considering that I want to add 25 players for the maximum I should copy my code (250 times) with an incremented value in order to gain the same functionality for each player.
I've thought about using an array or a hashmap I'm just not sure what's the best practise to do it. Any suggestions are more than welcome.
Pre-declared variables for one player:
private int counter1 = 0;
private int counter2 = 0;
private int counter3 = 0;
private int counter4 = 0;
private int counter5 = 0;
private int counter6 = 0;
private int counter7 = 0;
private int counter8 = 0;
private int counter9 = 0;
private int counter10 = 0;
private TextView textCounter1;
private TextView textCounter2;
private TextView textCounter3;
private TextView textCounter4;
private TextView textCounter5;
private TextView textCounter6;
private TextView textCounter7;
private TextView textCounter8;
private TextView textCounter9;
private TextView textCounter10;
Routine that needs to be copied 250 times:
if (playersAdded == 1) {
//Set 1
ImageButton decrementButton1 = new ImageButton(getApplicationContext());
decrementButton1.setImageDrawable(decrementDrawableScaled);
decrementButton1.setLayoutParams(layoutParams);
textCounter1 = new TextView(getApplicationContext());
textCounter1.setText(String.valueOf(counter1));
textCounter1.setLayoutParams(layoutParams);
ImageButton incrementButton1 = new ImageButton(getApplicationContext());
incrementButton1.setImageDrawable(incrementDrawableScaled);
incrementButton1.setLayoutParams(layoutParams);
decrementButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (counter1 > 0) {
counter1 -= 1;
textCounter1.setText(String.valueOf(counter1));
}
}
});
incrementButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter1 += 1;
textCounter1.setText(String.valueOf(counter1));
}
);
//End of Set 1
}
Upvotes: 1
Views: 76
Reputation: 91
Upvotes: 1