Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Android String Array if i have Array name in another String

I have a problem, a technical one. I have a list view, when I click an item I get content at that specific position and send it to next activity through 'putExtra'

On next activity what i am doing is

        String item;
        Bundle extras = getIntent().getExtras(); 
        if (extras != null) {
            item = extras.getString("item");
        }

Now lets say here item = "Java";

Now what I want is termsArray = getResources().getStringArray(R.array.Java);

But i donot want R.array.Java, I want some thing like R.array.item

String Arrays in XML

<string-array name="Java">
<item>A</item>
<item>B</item>
<item>C</item>
</string-array>

<string-array name="C">
<item>A</item>
<item>B</item>
<item>C</item>
</string-array>

<string-array name="php">
<item>A</item>
<item>B</item>
<item>C</item>
</string-array>

I can do this task using IF-else but I have many String Arrays, so it is not possible to use IF-else

like image 709
Romio Avatar asked Dec 05 '22 15:12

Romio


1 Answers

try as:

 String item;
        Bundle extras = getIntent().getExtras(); 
        if (extras != null) {
            item = extras.getString("item");
            int arryid = this.getResources().getIdentifier(item, "array",
                this.getPackageName()); 
            termsArray  = this.getResources().getStringArray(arryid);
        }
like image 164
ρяσѕρєя K Avatar answered Dec 28 '22 23:12

ρяσѕρєя K