Android Spinner control details: androidspinner Control
Spinner class diagram
Android. widget class Spinnerjava. lang. Object android. view. View android. view. ViewGroup android. widget. AdapterView <SpinnerAdapter> android. widget. AbsSpinner android. widget. Spinner
Spinner refers to the drop-down list component.
The content of this document is as follows.
Add a Spinner element label to the layout file.
<Spinner android:id="@+id/planets_spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" />
To populate the spinner with a list of choices, you then need to specifySpinnerAdapter
In yourActivity
OrFragment
Source code.
To add a selection item to the spinner, You need to bind a specific SpinnerAdapter object in your Activity or Fragment as a data source to the Spinner object.
The choices you provide for the spinner can come from any source, but must be provided throughSpinnerAdapter
, Such asArrayAdapter
If the choices are available in an array orCursorAdapter
If the choices are available from a database query.
Although you can provide any data source to the spinner, this type of data source must implement the SpinnerAdapter interface. For example, ArrayAdapter. If you select to query data from the database, you can use the Adapter such as CursorAdapter as the data source.
You can use a string array.
<?xml version="1.0" encoding="utf-8"?><resources> <string-array name="planets_array"> <item>Mercury</item> <item>Venus</item> <item>Earth</item> <item>Mars</item> <item>Jupiter</item> <item>Saturn</item> <item>Uranus</item> <item>Neptune</item> </string-array></resources>
With an array such as this one, you can use the following code in yourActivity
OrFragment
To supply the spinner with the array using an instanceArrayAdapter
:
If you use a string array, you need to use ArrayAdapter in your Activity or Fragment.
Spinner spinner = (Spinner) findViewById(R.id.spinner);// Create an ArrayAdapter using the string array and a default spinner layoutArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, android.R.layout.simple_spinner_item);// Specify the layout to use when the list of choices appearsadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// Apply the adapter to the spinnerspinner.setAdapter(adapter);
Static Method createFromResource (Context context, int textArrayResId, int textViewResId
)
The context parameter. Here, this indicates Activity.
The textArrayResId parameter uses the R. array. plants_array data source.
ParameterstextViewResId
Here, we use the layout style of android. R. layout. simple_spinner_item (which can be customized by android ).
You shoshould then callsetDropDownViewResource(int)
To specify the layout the adapter shocould use to display the list of spinner choices (simple_spinner_dropdown_item
Is another standard layout defined by the platform ).
You can call setDropDownViewResource (int) to specify the layout and style of the options in the spinner list.
How to respond to user selection and implement the OnItemSelectedListener Interface
public class SpinnerActivity extends Activity implements OnItemSelectedListener { ... public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { // An item was selected. You can retrieve the selected item using // parent.getItemAtPosition(pos) } public void onNothingSelected(AdapterView<?> parent) { // Another interface callback }}
TheAdapterView.OnItemSelectedListener
RequiresonItemSelected()
AndonNothingSelected()
Callback methods.
The interface must implement two methods.
Then you need to specify the interface implementation by callingsetOnItemSelectedListener()
(Specify the spinner object to implement the OnItemSelectedListener Interface)
Spinner spinner = (Spinner) findViewById(R.id.spinner);spinner.setOnItemSelectedListener(this);
How can I separate the displayed data from the selected value in the android spinner control?
If you want to use a spinner, we recommend that you define the encapsulated arrayAdapter of the adapter as inflexible and disgusting. If you customize the adapter, I feel that it is not difficult, directly List <Object> list = new ArrayList <Obejct> (); that object is a class similar to bin. You can use setValue (), and setId (); set your value, and then you can get the subscript, which is generally arg2, and then get the data through list. In getId (), it will be OK.
Android spinner control usage Problems
Put "Please select" in the first place of the list and set spinner. setSelection (0 );
In onItemClick, select whether or not the click is selected and process it by yourself logically.