Question

i have some consecutive elements id declared inside R.java files. Now i need to fill each one by using a for cycle, so i need to increment for each iteration value of id. I've write this:

int current_id = R.id.button00;
for (int i = 0; i < values.length; i++) {
    TextView to_fill = (TextView) (getActivity().findViewById(current_id));
    to_fill.setText(String.valueOf(values[i]));
    current_id++;
}

but in this way current_id doesn't increment correctly. How can i do?

No correct solution

OTHER TIPS

There is no guarantee that the generated IDs in R will be sequential, or that they will be generated in any particular order.

I recommend putting your IDs in an array resource like so:

<array name="button_id_array">
    <item>@id/button00</item>
    <item>@id/button01</item>
    <item>@id/button02</item>
</array>

Then you can access it in code like so:

int[] ids = getResources().getIntArray(R.array.test);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top