2012-07-27 50 views
3

我有一個問題,一個技術問題。我有一個列表視圖,當我點擊一個項目我會在那個特定位置的內容,並通過發送到下一個活動「putExtra」如何獲取Android字符串數組,如果我有另一個字符串中的數組名稱

在接下來的活動我在做什麼是

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

現在讓我們說這裏item =「Java」;

現在我想要的是 termsArray = getResources()。getStringArray(R.array.Java);

但我不想要R.array.Java,我想要一些像R.array一樣的東西。在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> 

我能做到使用IF-其他人,但我有很多字符串數組這個任務,所以不可能使用的if-else

+3

這可能會有所幫助http://stackoverflow.com/a/3044081/603744 – 2012-07-27 10:09:40

回答

15

嘗試爲:

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); 
     } 
+1

謝謝IMRA可汗 – Romio 2012-07-27 10:22:04

相關問題