2013-06-03 113 views
0

我試圖將項目添加到帶有循環的微調器,但無法使其工作。 XML閱讀器的一部分工作,我只需要填寫微調。我試圖修改那些在這裏被copypasted的代碼片段(使用simple_spinner_item的代碼片段),但沒有成功。Android:將項目添加到帶有循環的微調器

工作流程:

1) There's an empty spinner 
2) Delete all items in the spinner (in case I add items to it again) 
3) Parse XML 
4) Add the items from the XML to the spinner 

佈局:

<Spinner 
    android:id="@+id/SPI_Test" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
     android:prompt="@string/test_prompt" /> 

VIEW:

//SPINNER 
Spinner mySpinner = (Spinner) findViewById(R.id.SPI_Test); 


//CLEAR OUT SPINNER SOMEHOW 

//XML READER BOTTOM PART 
for (int j = 0; j < childNodes.getLength()-1; j++) { 
    Node item = childNodes.item(j); 
//SOMEHOW_ADD_TO_SPINNER=item.getTextContent(); 

回答

2

您需要更改適配器支持微調。事情是這樣的:

ArrayList<String> stringArrayList = new ArrayList<String>(); 

然後在你的for循環

stringArrayList.add(item.getTextContent()); 

然後交換適配器微調

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, stringArrayList); 
mySpinner.setAdapter(adapter); 
+0

謝謝,成功了!你知道爲什麼spinner看起來不同於通過strings.xml手動添加值嗎?當我使用手動方法時,列表項總是很大,旁邊有一個綠色的單選按鈕,列表是可滾動的。使用這種方法,文本更小,它們之間始終有一個空白條目,並且沒有單選按鈕。 – CodeBunny

+0

沒問題,您需要更改資源ID以更改微調器的外觀。玩android.R.layout。 或嘗試使用其他方法mySpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)等。我不記得確切,但其中一種方法應該可以幫到你。 –

+0

嗯,那也行,但是把我的「普通」微調按鈕變成了一個看起來像是彈出的微調列表中的項目的大按鈕。猜猜我會離開它! – CodeBunny

相關問題