Increase the ID of a TextView with a loop

0

I'm new to Android and I'm trying to make a 9x9 table that is full of TextViews in each cell. And I want to put numbers from an array with a setText () in each TextView.

The problem is that I do not know how to make the TextView id automatic.

I have the following:

    int z = 0;
    int contador = 0;

    for(int y = 1; y <= 9; y++) { //Filas
        for (int x = 1; x <= 9; x++) { //Columnas


            String nombreCF = "c"+x+"f"+y; //Id de los text view c1f1, c2f1, c3f1...

            contador = 0;

            for(z=z; contador < 1; z++){

                nombreCF.setText(items[z]); //ERROR nombreCF no se le puede poner un setText()
                contador=1;
            }

        }
    }

As you can see I tried to make the textview id a string with the number of row and column generated by a loop but of course a string does not have the function setText ().

I do not know if I'm making a mess but you know how to help me do it?

Thank you very much!

I forgot to put the array I want it to have. There must be a number by TextView.

  

static int [] items = {7,9,2,6,1,5,3,8,4,5,8,3,7,4,2,6,9,1,1,6, 4,3,9,8,5,2,7,9,4,8,2,6,3,7,1,5,2,7,5,4,8,1,9,6,3, 6,3,1,9,5,7,2,4,8,8,5,7,1,2,9,4,3,6,3,2,6,6,7,4,1, 5,9,4,1,9,5,3,6,8,7,2};

    
asked by rafemo 03.11.2018 в 21:48
source

1 answer

0

You can create the ids of the textviews with an int, since the resource is of that type, this prevents you from creating x TextViews in your layout.

final int N = 10; // Cantidad de textviews a crear

final TextView[] myTextViews = new TextView[N]; // Creamos un array vacio

for (int i = 0; i < N; i++) {
    // Creamos un nuevo textview
    final TextView rowTextView = new TextView(this);

    // ponemos algunas propiedades al TextView
    rowTextView.setText("This is row #" + i);

    // Añadimos el textview al linear layout
    myLinearLayout.addView(rowTextView);

    // guardamos la referencia donde se guardo el texto para accederlo luego
    myTextViews[i] = rowTextView;
}

In this way we create TextViews dynamically, and we assign them to LinearLayout to be able to visualize them.

    
answered by 04.11.2018 в 07:23