Question

I set the spinner list adapter items with

myAdapter = ArrayAdapter.createFromResource(MyActivity.this, R.array.my_array.xml, R.layout.spinner_item);

Now I want to use an array created programmatically (not in xml)

String myArray[]={somethinggenerated};

if I try

myAdapter = ArrayAdapter.createFromResource(MyActivity.this, myArray, R.layout.spinner_item);

doesn't work because doesn't match with the method.

How could I solve my problem?

Was it helpful?

Solution

Creates a new ArrayAdapter from external resources. The content of the array is obtained through getTextArray(int).

 ArrayAdapter.createFromResource(MyActivity.this, R.array.my_array.xml, R.layout.spinner_item);

In your case you have to use below way :-

new ArrayAdapter<String>(this, R.layout.spinner_layout,  myArray);

OTHER TIPS

 new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,myArray);

You can use an arraylist instead and attach it to the adapter as follows:

        List<String> list = new ArrayList<String>();
        list.add("list1");
        list.add("list2");
        list.add("list3");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top