Let’s expand the choices.
How about Bach?
Here’s the code.
package com.allmycode.a12_11;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
textView = (TextView) findViewById(R.id.textView);
String[> choices =
{"Select a composer",
"Monteverdi", "Pachelbel", "Corelli", "Albinoni",
"Vivaldi", "Telemann", "Handel","Bach", "Scarlatti"};
<strong> ArrayAdapter<String> adapter =</strong>
<strong> new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, choices);</strong>
<strong> </strong>
<strong> spinner.setAdapter(adapter);</strong>
<strong> spinner.setOnItemSelectedListener(new MyItemSelectedListener());</strong>
}
<strong> class MyItemSelectedListener implements OnItemSelectedListener {</strong>
<strong> </strong>
<strong> @Override</strong>
<strong> public void onItemSelected(AdapterView<?> adapterView, View view,</strong>
<strong> int position, long id) {</strong>
<strong> </strong>
<strong> if (position == 0) {</strong>
<strong> textView.setText("You haven't selected a composer.");</strong>
<strong> } else {</strong>
<strong> textView.setText(adapterView.getItemAtPosition(position).toString());</strong>
<strong> }</strong>
<strong> }</strong>
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
// Do nothing
}
<strong>}</strong>
}
To make a spinner do its job, you create a listener and an adapter.
The listener
A spinner's listener is much like a button's listener. It's a piece of code that listens for user actions and responds when an appropriate action occurs.In the code above, a listener is created (an instance of MyItemSelectedListener
class). You tell Android to notify the listener when the user selects one of the spinner's items:
spinner.setOnItemSelectedListener(new MyItemSelectedListener());
The MyItemSelectedListener
class's onItemSelected
method must tell Android what to do in response to the user's selection.
The adapter
You may guess that you add an item to a spinner with a call like this:<strong>// Don't do this:</strong>
spinner.addRow("Monteverdi");
But that's not the way it works. When an Android developer thinks about a spinner, the developer thinks about two different concepts:
- A spinner has data.
The spinner's data consists of the values "Select a composer"
, "Monteverdi"
, "Pachelbel"
, and so on.
- A spinner has a “look.”
This section's spinner has a simple look. In the first image, the spinner has text on the left side and a tiny downward arrow on the right side. In the second image, each of the spinner's items has text on the left side.
A spinner's incarnation on the screen (the “look”) is an object in and of itself. It's an instance of Android'sAdapterView
class. A similar-sounding thing, an instance of the SpinnerAdapter
class, connects a spinner's data with a spinner's “look.”There are several kinds of spinner adapter, including the ArrayAdapter
and CursorAdapter
classes:
- An
ArrayAdapter
gets data from a collection, such as an array or anArrayList
. - A CursorAdapter gets data from a database query.
ArrayAdapter
is used. The ArrayAdapter
constructor has three parameters:
- The first parameter is a context.
Use this
for the context. The word this
represents whatever object contains the current line of code. In the code, this
refers to the MainActivity
.
- The second parameter is a layout.
The name android.R.layout.simple_spinner_item
refers to a standard layout for one of the items.
- The third parameter is the source of the data.
You can provide choices, which you declare to be an array of String values.
Notice theonItemSelected
method's position
parameter. When the user selects the topmost item in the spinner's list (the Select a Composer item), Android gives that position
parameter the value 0. When the user selects the next-to-topmost item (the Monteverdi item), Android gives that position
parameter the value 1. And so on.In the onItemSelected
method's body, the code checks to make sure that position
isn't 0. If position
isn't 0, the code plugs that position
value into the adapterView.getItemAtPosition
method to get the string on whatever item the user clicked. The code displays that string (Monteverdi
, Pachelbel
, or whichever) in a textView
component.