Dave's Brain

Browse - Programming Tips - How do I load a Spinner with a finite set of choices?

Date: 2017feb9
OS: Android
Language: Java

Q.  How do I load a Spinner with a finite set of choices?

A.  Hear an example that lets the user pick a kind of fruit.

void loadFruitSpinner(Context context, Spinner spinner, final String strCurrentFruit) { ArrayList<String> list = new ArrayList<String>(Arrays.asList("apple", "banana", "pear")); int selected = -1; for (int i = 0; i < list.size(); i++) { if (list.get(i).equalsIgnoreCase(strCurrentFruit))) selected = i; } ArrayAdapter<String> adapter = new ArrayAdapter<String> (context, android.R.layout.simple_spinner_item, list); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setSelection(selected); }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.