1

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:

Table of the application

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
}
2
  • Use an array. Commented Aug 30, 2016 at 8:30
  • 1
    Or an arraylist Commented Aug 30, 2016 at 8:31

1 Answer 1

1
  1. Make class Player. (Learn Object-oriented programming)
  2. Make row for one player. (Learn how to create listView, this tutorial should help you, or just google android listView)
  3. Make listView with players row.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.