如何将ArrayList <string>传递给String []并在Spinner中显示值?

时间:2017-06-15 09:19:04

标签: java android string arraylist spinner

我尝试做的是以下内容,我用ArrayList<String>填充了数据,然后我有一个String[]我正在使用在我的微调器上显示文本,所以我想把我的ArrayList的数据放在里面,以默认替换什么,但没有任何反应。

当我试图在没有Arrays.toString()的情况下显示它时,它给了我以下内容:

[Ljava.lang.string;@e4b348

但是使用arrays.tostring()我得到了我想要的东西。

那么如何在我的string []中传递我的数据ArrayList以在spinner上显示它?

这是我的代码:

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) {}
            };

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

You don't need to convert your arrayList of Strings into an Array when using it with spinner adapter.

See code below -

    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);

This is what I get -

enter image description here

答案 1 :(得分:0)

Try this for convert Arraylist to array

String[] textfordropdown = arrayList.toArray();

and then set it to Spinner like that

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 );