Android- Multiple Spinners
android, android-layout, android-widget, spinner
Solution
You could share the adapter between different `Spinner`s if they adapted the same information. Clearly each of your adapters need to adapt a different set of `String`s, so you need to create an `ArrayAdapter` for each `Spinner`.
A single `OnItemSelectedListener` will work for the 3 `Spinners` (as long as you set them). You can call `getId()` on the `AdapterView<?>` passed as an argument to know which `Spinner` raised the event.
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
switch(arg0.getId()){
case R.id.s_answertime:
break;
case ...
}
}
Problem
I have this code: ``` package lijap.app.starcraft2counters; import java.io.File; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; public class Settings extends Activity implements OnItemSelectedListener { Spinner answertime; Spinner gametime; Spinner missesallowed; String[] answerseconds = { "Unlimited", "1 second", "2 seconds", "3 seconds", "4 seconds", " 5 seconds" }; String[] gameminutes = { "Unlimited", "1 minute", "2 minutes", "3 minutes", "4 minutes", " 5 minutes" }; String[] numberofmisses = { "Unlimited", "5", "10", "15", "20", "25" }; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.settings); ArrayAdapter<String> adapter = new ArrayAdapter<String>(Settings.this, android.R.layout.simple_spinner_item, gameminutes); answertime = (Spinner) findViewById(R.id.s_answertime); answertime.setAdapter(adapter); answertime.setOnItemSelectedListener(this); } @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } } ``` I am getting confused trying to put in more than 1 spinner. Do I share the array adapter, or do I create a new one for each? What about the onItemSelected? As you can see from my strings at the top, I want three spinners. How would I do this? I am new to android, as you can tell. All help is appreciated- Lijap