Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access variables by name in a loop

Tags:

java

android

I'm working on an Android project, and i have a lot of drawables. These drawables are all named like icon_0.png, icon_1.png ... icon_100.png. I want to add all the resource id's of these drawables to an ArrayList of Integers. (For those, who do not know android, only Java, i am talking about static variables, in a static inner class of a class, like R.drawable.icon_0. All of this static variables are Integers.)

Is there a more efficient way to do this, than adding them one by one? Like

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(R.drawable.icon_1);
list.add(R.drawable.icon_2);
...
list.add(R.drawable.icon_100);

Can i loop through them somehow? Like

for(int i=0; i<100; i++)
{
    list.add(R.drawable.icon_+i);  //<--- I know this doesn't work.
}

I have no control over the file where these static integers are, and i cannot create the drawables in runtime.

Any help would be appreciated!

EDIT

Okay, i read the answers, but i have one major problem: I don't have access to any Context instances where i need to create this array/list of ids (i do it in a static initialzer block), so the getResources() method, what two of the answers suggested wont work. Is there any other way of doing this?

like image 766
Balázs Édes Avatar asked Aug 05 '12 06:08

Balázs Édes


People also ask

Can you call variables in a for loop?

Any variables defined within loops are limited to the scope of that loop and cannot be called from outside of the loop. You can call variables that are defined outside of a loop, from within a loop, but not the other way around.

How do you access variable names?

Retrieve variable names directly, using nameof. Detect next immediate attribute name, using will. Fetch argument names/sources passed to a function using argname.

How do you do a for loop with multiple variables?

And you, too, can now declare multiple variables, in a for-loop, as follows: Just separate the multiple variables in the initialization statement with commas. Do not forget to end the complete initialization statement with a semicolon.

How do you make a variable name dynamic in Python?

Using exec() method to create dynamically named variables Here we are using the exec() method for creating dynamically named variable and later assigning it some value, then finally printing its value.


1 Answers

Create an XML file in the values folder in your resource directory.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="myIcons">
    <item>@drawable/icon1</item>
    <item>@drawable/icon2</item>
    <item>@drawable/icon3</item>
    <item>@drawable/icon4</item>
    <item>@drawable/icon5</item>
    ...
    ...
</array>
</resources>

Go through the following code, you will get the idea.

Resources res = getResources();
TypedArray myIcons= res.obtainTypedArray(R.array.myIcons);  //mentioned  in the XML
for(int i=0; i<100; i++)
{
    Drawable drawable = myIcons.getDrawable(i);
    list.add(drawable);  
}
like image 152
Rahmathullah M Avatar answered Oct 17 '22 16:10

Rahmathullah M