用Java-Android中的循环替换预先声明的变量

用Java-Android中的循环替换预先声明的变量,java,android,Java,Android,我正在为我的一个足球教练朋友开发一个android应用程序 他让我创建一个应用程序,它更像是一个统计球员统计数据的表格。下面的图片应该会让事情变得清晰起来: 正如你所看到的,每次我按下左上角的按钮,我都想添加一个新的玩家,两个,一个新的行,一个减量按钮,一个计数器和一个递增按钮,在每个统计列下面 我现在可以这样做,但每个玩家有10个不同的列计数器。因此,考虑到我想最多添加25个玩家,我应该复制我的代码(250次),并增加一个值,以便为每个玩家获得相同的功能 我曾考虑过使用数组或哈希映射,但我不

我正在为我的一个足球教练朋友开发一个android应用程序

他让我创建一个应用程序,它更像是一个统计球员统计数据的表格。下面的图片应该会让事情变得清晰起来:

正如你所看到的,每次我按下左上角的按钮,我都想添加一个新的玩家,两个,一个新的行,一个减量按钮,一个计数器和一个递增按钮,在每个统计列下面

我现在可以这样做,但每个玩家有10个不同的列计数器。因此,考虑到我想最多添加25个玩家,我应该复制我的代码(250次),并增加一个值,以便为每个玩家获得相同的功能

我曾考虑过使用数组或哈希映射,但我不确定最好的做法是什么。欢迎提出任何建议

为一个玩家预先声明的变量:

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;
需要复制250次的例程:

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
}
  • 让班级成为一名球员。(学习面向对象编程)
  • 为一名球员划船。(了解如何创建listView,应该对您有所帮助,或者只是谷歌android listView)
  • 使用玩家行生成listView
  • 使用。或