2012-01-04 19 views
5

我想從strings.xml中的另一個字符串數組引用一個字符串數組。如果我嘗試在我的活動中讀取字符串數組(名爲「plants」),則每個項目的值都爲null。在android strings.xml中引用字符串數組

是否有可能獲得這些值?

這裏是strings.xml中的一部分:

<string name="Orlaya_grandiflora_1">Orlaya grandiflora</string> 
<string name="Orlaya_grandiflora_2">Large-Flowered Orlaya</string> 
<string name="Orlaya_grandiflora_3">Apiaceae</string> 
<string-array name="Orlaya_grandiflora"> 
    <item>@string/Orlaya_grandiflora_1</item> 
    <item>@string/Orlaya_grandiflora_2</item> 
    <item>@string/Orlaya_grandiflora_3</item> 
</string-array> 
<string-array name="plants"> 
    <item>@array/Ginkgo_biloba</item> 
    <item>@array/Capsicum_frutescens</item> 
    <item>@array/Viscum_album</item> 
    <item>@array/Orlaya_grandiflora</item> 
</string-array> 

我嘗試訪問這樣的值,例如:

String[] plantArray = resources.getStringArray(R.array.plants); 

for (String plant : plantArray) { 
     System.out.println("--> " + plant); 
    } 

工廠的值是在任何情況下「空「

任何人都知道如何訪問這些值? Android - String Resources

enter image description here

+0

什麼是無害的「資源」?對象還是其他? – 2012-01-04 13:33:13

+0

這是缺少:Resources resources = getResources(); – alex3000 2012-01-04 13:46:31

回答

6

你應該做到以下幾點:

+0

謝謝,這就是我一直在尋找! – alex3000 2012-01-04 14:05:46

+0

好的。如果這是正確的,你可以接受答案。 – 2012-01-04 14:08:32

21

直例子給出

//obtain the array that refrences others array 
    TypedArray plantArray=getResources().obtainTypedArray(R.array.plants); 
    //obtain the referenced arrays 
    CharSequence[] ginkoArray=plantArray.getTextArray(0); 
    //... 
    CharSequence[] orlayaArray=plantArray.getTextArray(3); 
+1

這不適用於字符串數組的引用,因爲我寫了這個字符 – alex3000 2012-01-04 13:36:35

+0

爲什麼要將字符串數組放在字符串數組中?這甚至有可能嗎?對我來說,似乎只能將字符串資源放在項目中而不是字符串數組中。 – eMich 2012-01-04 13:39:32

+2

@eMich我已經提到過將字符串數組放入字符串數組中? – 2012-01-04 13:41:10

1
public class Test extends ListActivity { 
    String[] presidents; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    ListView lstView = getListView(); 
    // lstView.setChoiceMode(0); //CHOICE_MODE_NONE 
    // lstView.setChoiceMode(1); //CHOICE_MODE_SINGLE 
    lstView.setChoiceMode(2); // CHOICE_MODE_MULTIPLE 
    lstView.setTextFilterEnabled(true); 

    presidents = getResources().getStringArray(R.array.presidents_array); 

    setListAdapter(new ArrayAdapter<String>(this, 
     android.R.layout.simple_list_item_checked, presidents)); 
    } 

    public void onListItemClick(ListView parent, View v, int position, long id) { 
    parent.setItemChecked(position, parent.isItemChecked(position)); 

    Toast.makeText(this, "You have selected " + presidents[position], 
     Toast.LENGTH_SHORT).show(); 
    } 
}; 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 



</LinearLayout> 

strings.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="hello">Hello World, MainActivity!</string> 
    <string name="app_name">BasicViews5</string> 
    <string-array name="presidents_array"> 
     <item>Dwight D. Eisenhower</item> 
     <item>John F. Kennedy</item> 
     <item>Lyndon B. Johnson</item> 
     <item>Richard Nixon</item> 
     <item>Gerald Ford</item> 
     <item>Jimmy Carter</item> 
     <item>Ronald Reagan</item> 
     <item>George H. W. Bush</item> 
     <item>Bill Clinton</item> 
     <item>George W. Bush</item> 
     <item>Barack Obama</item> 
    </string-array> 
</resources> 
+0

+1好回答:) – 2013-12-31 17:36:16

相關問題