7 package com.google.appinventor.components.runtime;
9 import android.os.Build.VERSION;
10 import android.os.Build.VERSION_CODES;
11 import android.view.View;
12 import android.widget.AdapterView;
13 import android.widget.AdapterView.OnItemSelectedListener;
14 import android.widget.ArrayAdapter;
39 @DesignerComponent(version = YaVersion.SPINNER_COMPONENT_VERSION,
40 description =
"<p>A spinner component that displays a pop-up with a list of elements." +
41 " These elements can be set in the Designer or Blocks Editor by setting the" +
42 "<code>ElementsFromString</code> property to a string-separated concatenation" +
43 " (for example, <em>choice 1, choice 2, choice 3</em>) or by setting the " +
44 "<code>Elements</code> property to a List in the Blocks editor. " +
45 "Spinners are created with the first item already selected. So selecting " +
46 " it does not generate an After Picking event. Consequently it's useful to make the " +
47 " first Spinner item be a non-choice like \"Select from below...\". </p>",
48 category = ComponentCategory.USERINTERFACE,
50 iconName =
"images/spinner.png")
54 private final android.widget.Spinner view;
55 private ArrayAdapter<String> adapter;
57 private int oldAdapterCount;
58 private int oldSelectionIndex;
66 if (VERSION.SDK_INT < VERSION_CODES.HONEYCOMB) {
73 adapter =
new ArrayAdapter<String>(
container.
$context(), android.R.layout.simple_spinner_item);
74 adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
75 view.setAdapter(adapter);
76 view.setOnItemSelectedListener(
this);
92 @
SimpleProperty(description =
"Returns the current selected item in the spinner ",
102 @
SimpleProperty(description =
"Set the selected item in the spinner",
111 @
SimpleProperty(description =
"The index of the currently selected item, starting at 1. If no " +
123 @
SimpleProperty(description =
"Set the spinner selection to the element at the given index." +
124 "If an attempt is made to set this to a number less than 1 or greater than the number of " +
125 "items in the Spinner, SelectionIndex will be set to 0, and Selection will be set to empty.",
135 @
SimpleProperty(description =
"returns a list of text elements to be picked from.",
144 @
SimpleProperty(description =
"Adds the passed text element to the Spinner list",
149 if (itemList.size() == 0) {
155 setAdapterData(itemList.toStringArray());
163 @
SimpleProperty(description =
"Sets the Spinner list to the elements passed in the " +
169 private void setAdapterData(String[] theItems) {
170 oldAdapterCount = adapter.getCount();
172 for (
int i = 0; i < theItems.length; i++){
173 adapter.add(theItems[i]);
180 @SimpleProperty(description =
"Text with the current title for the Spinner window",
181 category = PropertyCategory.APPEARANCE)
183 return view.getPrompt().toString();
190 @
SimpleProperty(description =
"Sets the Spinner window prompt to the given title",
196 @
SimpleFunction(description =
"Displays the dropdown list for selection, " +
197 "same action as when the user clicks on the spinner.")
205 @
SimpleEvent(description =
"Event called after the user selects an item from the dropdown list.")
210 public void onItemSelected(AdapterView<?> parent, View view,
int position,
long id){
217 if (oldAdapterCount == 0 && adapter.getCount() > 0 && oldSelectionIndex == 0 ||
218 oldAdapterCount > adapter.getCount() && oldSelectionIndex > adapter.getCount()) {
220 oldAdapterCount = adapter.getCount();
228 view.setSelection(0);