0

I have a layout for menu options(like settings). I can't set the value for the spinner in my main Activity. How to do this in android. I am using eclipse

I can get the spinner by using following code in onCreate method. but I can't set the values i didn't get any error

LayoutInflater inflater = (LayoutInflater) MyExpensive.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.chartconfiguration,
                (ViewGroup) findViewById(R.id.chartConfig));
        Spinner spinner = (Spinner)layout.findViewById(R.id.spinner2);

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, android.R.array.organizationTypes);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);
Anbu
  • 111
  • 2
  • 4
  • Possible duplicate of [Android: Create spinner programmatically from array](http://stackoverflow.com/questions/2784081/android-create-spinner-programmatically-from-array) – Hugo Jul 18 '16 at 11:54

1 Answers1

0

Define the content of the Spinner by a String-Array in your strings.xml file:

  <string-array name="myspinnercontent">
        <item>A</item>
        <item>B</item>
        <item>C</item>
  </string-array>

And now create the adapter using ArrayAdapter.creaFromResource method:

ArrayAdapter<CharSequence> myAdapter = ArrayAdapter.createFromResource(this,
                R.array.myspinnercontent, android.R.layout.simple_spinner_item);
SamuelPS
  • 555
  • 6
  • 20