2017-08-22 39 views
0
private Spinner mSpnrCustomer; 
private List<String> mCustomerList = new ArrayList<String>(); 
mCustomerList= Arrays.asList(getResources().getStringArray(R.array.customer_names)); 
mSpnrCustomer.setAdapter(new SpinnerRoomTypeAdapter(context,mCustomerList)); 

此處,Spinner已成功填充ArrayList中的相應數據。我想添加一個額外的字符串,而不是ArrayList底部的內容,例如「CreateUser」。選擇這個我需要它來打開一個彈出窗口。我怎樣才能做到這一點?如何在Spinner中添加額外的字符串

+1

之前嘗試添加 –

+0

請發表您的logcat,並在該行呢拋出異常後適配器做notifydatasetchanged? – AndiM

回答

0

需要notify適配器大約在列表中的變化

mCustomerList = new ArrayList<String>(); 
//populate the list 
mCustomerList = 
Arrays.asList(getResources().getStringArray(R.array.customer_names)); 
//set the adapter 
mSpnrCustomer.setAdapter(new SpinnerRoomTypeAdapter(context,mCustomerList)); 
//add something to the List 
mCustomerList.add("Foo"); 
//notify the adapter 
myAdapter.notifyDataSetChanged(); 

現在你有你設置的適配器作爲一個匿名實例的問題......那麼你將需要一個變量爲

+0

我越來越java.lang.UnsupportedOperationException – Noufal

0

其簡單:

private Spinner mSpnrCustomer; 
private List<String> mCustomerList = new ArrayList<String>(); 
    mCustomerList= Arrays.asList(getResources().getStringArray(R.array.customer_names)); 
String createUser= "CreateUser"; 
mCustomerList.add(createUser); 
     mSpnrCustomer.setAdapter(new SpinnerRoomTypeAdapter(context,mCustomerList)); 

說明:您有保存String對象的ArrayList秒。如果你想添加新的字符串,你可以使用ArrayList方法添加來實現它。

+0

試過,但越來越java.lang.UnsupportedOperationException – Noufal

0

嘗試此

List<String> list = new LinkedList<>(Arrays.asList(getResources().getStringArray(R.array.customer_name))); 
list.add("CreateUser"); 

mSpnrCustomer.setAdapter(new SpinnerRoomTypeAdapter(context,list)); 
+0

我alredy試過,但有強制關閉 – Noufal

+0

@Noufal請立即檢查,你得到unsupportedoperation例外,因爲Arrays.asList()返回一個固定大小列表中,您不能在此添加元素 –

0
private Spinner mSpnrCustomer; 
private List<String> mCustomerList = new ArrayList<String>(); 
customer_names.add("Value"); 
    mCustomerList= Arrays.asList(getResources().getStringArray(R.array.customer_names)); 
     mSpnrCustomer.setAdapter(new SpinnerRoomTypeAdapter(context,mCustomerList)); 

添加一行代碼customer_names.add( 「值」);調用微調

相關問題