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
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<?>
Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one. You can add a spinner to your layout with the Spinner object.
Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it.
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 ...
}
}
Check this one
yearDropdown.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
selectedyear = parentView.getSelectedItemPosition();
drpyear = yearDropdown.getSelectedItem().toString();
//your code here
}
public void onNothingSelected(AdapterView<?> parentView)
{
selectedyear = 0;
//return;
}
});
monthDropdown.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
//your code here
}
public void onNothingSelected(AdapterView<?> parentView)
{
selectedmonth = 0;
//return;
}
});
dayDropdown.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
//your code here
}
public void onNothingSelected(AdapterView<?> parentView)
{
selectedday = 0;
}
});
you need to create each adapter for every spinner.
adapterYear = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item);
yearDropdown.setAdapter(adapterYear);
adapterMonth = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item);
monthDropdown.setAdapter(adapterMonth);
adapterDays = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item);
dayDropdown.setAdapter(adapterDays);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With