Browse items by name, android

1

I am doing a survey project which implements many RadioButton . Within this project I am implementing a functionality that retrieves the status of the interface using SQLite database, so I need to select RadioButton with respect to what was recorded. By this I mean:

If I have 100 Radiobutton they have a name like this:

(Radio_1, Radio_2, Radio_3, Radio_4, Radio_5.......Radio_100); 

If I want to change the status of each one, I would do it in the following way:

findViewById(R.id.Radio_1).setChecked(true);
findViewById(R.id.Radio_2).setChecked(true);
findViewById(R.id.Radio_3).setChecked(true);
.
.
.
.
findViewById(R.id.Radio_100).setChecked(true);

But, is there any way to put this in a For cycle? So the code would be much more effective

    
asked by Felipe Mendieta Perez 12.04.2017 в 20:30
source

1 answer

0

First create an array containing the resources, obviously it would be an array of integer values:

 int[] elementos = {R.id.Radio_1, R.id.Radio_2, R.id.Radio_3};

It is necessary to add the existing ids within the project since they are related to a value within the file R.java .

You would walk in this way to assign the new status to all the elements:

  for(int i = 0; i < elementos.length ; i++){
        ((RadioButton)findViewById(elementos[i])).setChecked(true);
    }
    
answered by 12.04.2017 в 23:32