2017-06-15 33 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); 
相关问题