1

This code helps me to load an array by clicking a button that has a tag name which is same with an array name. I know this approach is a headache because there is over 100 buttons,

 private void altBasamakGetir(String kazanimNO)
    {
        Resources res=this.getResources();
        infoTxt.setText(kazanimNO);
        switch (kazanimNO)
        {
            case "kazanim_0":
            {
                String  altBasamaklar[]= res.getStringArray(R.array.kazanim_0);
            }
            ;break;
            case "kazanim_1":
            {
                String  altBasamaklar[]= res.getStringArray(R.array.kazanim_1);
            }
            ;break;
            case "kazanim_2":
            {
                String  altBasamaklar[]= res.getStringArray(R.array.kazanim_2);
            }
            ;break;
            case "kazanim_3":
            {
                String  altBasamaklar[]= res.getStringArray(R.array.kazanim_2);
            }
            ;break;
        }

    }
I want something like this :paramater to array name
 String  altBasamaklar[]= res.getStringArray(R.array.kazanimNo);

an easy way to engage the parameter kazanimNO to the array

I don't know how to search about this issue so I ask it here.

Thanks for any information.

Metin Ilhan
  • 120
  • 10

1 Answers1

2

What you are possibly looking for could be achieved with Reflection. However, I will give an example to do it in code with a cleaner way than a switch.

I would suggest using the Java Map interface. For example HashMap. Somewhere you will still have to initialize the values. The Map would use a String key and String[] value. Create a void function to initialize it.

public void initMap(HashMap<String, String[]> map)
{
  map.put("kazanim_0", Resources.getStringArray(R.array.kazanim_0));
  map.put("kazanim_1", Resources.getStringArray(R.array.kazanim_1));
  ....
}

Then you can get values with

String[] myArray = map.get("kazanim_0);
Community
  • 1
  • 1
milez
  • 2,201
  • 12
  • 31
  • Thank you very much,I don't wan to initialize values, isn'nt there something like ** R.array.getArrayByName(string)** – Metin Ilhan Aug 02 '15 at 11:14
  • Sadly I dont think there is, you would have to make your own. You could look into reflection but I'm not very familiar with it: http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful – milez Aug 02 '15 at 11:18