2015-05-18 24 views
1

我有一個custom spinner它正在替換我的默認spinner,該數據由我的strings xml中的硬編碼值填充。從字符串xml數組填充自定義微調器

我用我的custom spinner and populated it dynamically in java但我並不需要爲這一個做到這一點。

我必須填充一個列表,然後將它添加到Java中的微調框?如果是的話,我如何填充從strings.xml數組元素的列表?這裏

List<String> spinnerList = new ArrayList<String>(); 
    //spinnerList.addAll(R.array.array_spinner);error here, doesnt like this????????????? 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.spinner_custom, spinnerList); 
    spinnerSports.setAdapter(adapter); 

<resources> 
    <string-array name="array_custspinner"> 
     <item>item1</item> 
     <item>item2</item> 
    </string-array> 
</resources> 

回答

1

//spinnerList.addAll(R.array.array_spinner);error,那並不像 這?????????????

是的,它是正確的。 addAll期待您的ListString相同類型的Collection。但是你提供了一個int,你想使用的數組的ID。使用

List<String> spinnerList = new ArrayList<String> 
     (Arrays.asList(getResources().getStringArray(R.array.array_custspinner))); 

這樣你會得到一個修改List<String>包含在你所array_custspinner

定義的所有字符串