How to set Android Spinner data (array) from Java code

As a quick note, this source code shows how to set the data (array) on an Android Spinner from Java code:

// set the spinner data programmatically
Spinner qbSpinner1 = (Spinner)qbTableLayout.findViewById(R.id.spinner1);
List<Integer> spinnerArray = new ArrayList<>();
spinnerArray.add(10);
spinnerArray.add(11);
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(
        getActivity(),
        android.R.layout.simple_spinner_item,
        spinnerArray
);
//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
qbSpinner1.setAdapter(adapter);

I use that code in the onCreateView method of an Android Fragment.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.