2017-06-15 122 views
-1

我想要做的是以下,我有一個ArrayList<String>我填充數據,然後我有一個String[],我用來顯示我的微調上的文本,所以我想把我的數據ArrayList裏面,以取代默認情況下,但沒有任何反應。如何將ArrayList <String>傳遞給String []並在Spinner中顯示值?

當我試圖不Arrays.toString顯示它(),它給了我下面的:

[Ljava.lang.string;@e4b348 

但隨着arrays.tostring()我得到了我想要的東西。

那麼如何在我的字符串[]中傳遞我的數據ArrayList以將其顯示在微調器上?

這裏是我的代碼:

String prestationuserlist; 
ArrayList<String> textfordropdown2 = new ArrayList<>(); 
String[] textfordropdown = {"test", "test", "test","test", "test", "test","test", "test", "test","test", "test", "test","test", "test", "test","test", "test", "test"}; 
String[] valueofdropdowtext = {"test", "test", "test"}; 
Spinner spinnerdynamic; 


for(int i=0;i<arrSize;i++) { 
    object = prestationlist.getJSONObject(i); 
    // Here, we populate the array with value of json, each text and db value 
    textfordropdown2.add(object.getString("Text")); 
    valueofdropdown.add(object.getString("Value")); 
    valueofdropdowtext = valueofdropdown.toArray(new String[0]); 
    textfordropdown = textfordropdown2.toArray(new String [textfordropdown2.size()]); 

} 

我的微調:

//Start Create a spinner with different view and value. 
    spinnerdynamic = (Spinner)findViewById(R.id.dynamic_spinner); 
    ArrayAdapter<String> adapter1 = 
      new ArrayAdapter<String>(MainActivity.this, 
        android.R.layout.simple_spinner_item, textfordropdown);// Here we set the text with API return value. 
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// Here we create a dropdown list item. 
    spinnerdynamic.setAdapter(adapter1); 
    spinnerdynamic.setOnItemSelectedListener(onItemSelectedListener1); // Here we call the function which is getting the value according to selected text. 
    //End Create a spinner with different view and value. 

這裏是OnItemSelectedListener:

OnItemSelectedListener onItemSelectedListener1 = 
      new OnItemSelectedListener(){ 

       @Override 
       public void onItemSelected(AdapterView<?> parent, View view, 
              int position, long id) { 
        prestationuserlist = valueofdropdowtext[position]; 
        test = textfordropdown[position]; 
       } 

       @Override 
       public void onNothingSelected(AdapterView<?> parent) {} 
      }; 

感謝您的幫助!

+1

您面臨的問題是什麼?你的問題不清楚。 – gprathour

+0

問題是我的值「test」沒有被ArrayList的值替換textfordforddown2 –

+1

爲什麼不直接將'ArrayList '傳遞給Adapter?爲什麼你需要更換它? – gprathour

回答

0

在使用spinner適配器的時候,您不需要將String的arrayList轉換爲數組。

下面

見代碼 -

spinner = (Spinner) findViewById(R.id.spinner); 

    // populate arraylist with data 
    ArrayList<String> spinnerArrayList = new ArrayList<>(); 
    for(int i=1; i<10; i++) 
    { 
     spinnerArrayList.add("Item " + i); 
    } 

    // create spinner adapter with the above arraylist 
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_item, spinnerArrayList); 
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

    // attach adapter to spinner 
    spinner.setAdapter(dataAdapter); 

這是我得到的 -

enter image description here

+0

是的,但因爲我正在做一個治療,讓我的下拉菜單有不同的顯示和價值,我不能這樣做,如果我通過數組我得到我的文本,但然後值不再設置,其他方式我得到的價值,但無法顯示我的文本:/ –

+0

在這種情況下,您可以使用arraylist存儲「顯示的值」和散列圖存儲「顯示值+實際值」作爲鍵值對。然後,您可以使用類似下面的方式使用Hashmap獲取實際值 - (Map.Entry entry:hash.entrySet())String displayedValue = entry.getKey(); String actualValue = entry.getKey(); //使用實際值 } –

+0

使用bean或HashMap存儲鍵的文本和值的值! – lihongxu

0

嘗試此轉換ArrayList的數組

String[] textfordropdown = arrayList.toArray(); 

,然後將其設置爲微調像那

spinnerdynamic = (Spinner)findViewById(R.id.dynamic_spinner); 
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, textfordropdown); 
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
spinnerdynamic.setAdapter(arrayAdapter); 
相關問題