Im new to android. i have tried to built an application which has 2 spinner... i have few doubts to clear before going deep to my application..
please give brief answer to my question...
setOnItemSelectedListener(this)
for both?spinner.getSelectedItemPosition();
on both the spinners?waiting for ur valuable replies. reference materials are also appreciable
Yes, you can have multiple spinner's that can be managed by single listener or you can have seperate Listener for each.
For Single Listener you can do something like this,
Set the Listener
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
Then you can use onItemSelected
to get perform the action related to the spinner's.
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position,long arg3)
{
int id = parent.getId();
switch (id)
{
case R.id.spinner1:
// your stuff here
break;
case R.id.spinner2:
// your stuff here
break;
}
Check this:
Spinner spinner0, spinner1, spinner2; TextView textView0, textView1, textView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView0 = (TextView) findViewById(R.id.text0);
spinner0 = (Spinner) findViewById(R.id.spinner0);
ArrayAdapter<String> adapter0 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, text0);
adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner0.setAdapter(adapter0);
spinner0.setOnItemSelectedListener(onItemSelectedListener0);
textView1 = (TextView) findViewById(R.id.text1);
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, text1);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setSelection(0,false); //make sure only one listener is
//active in the onCreate method by setting the other listener as false spinner1.setOnItemSelectedListener(onItemSelectedListener1);
textView2 = (TextView) findViewById(R.id.text2);
spinner2 = (Spinner) findViewById(R.id.spinner2);
MySpinnerAdapter adapter2 = new MySpinnerAdapter(MainActivity.this, android.R.layout.simple_spinner_item, obj2);
//adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setSelection(0,false);
spinner2.setOnItemSelectedListener(onItemSelectedListener2);
}
OnItemSelectedListener onItemSelectedListener0 = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do your stuff spinner0
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
OnItemSelectedListener onItemSelectedListener1 = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do your stuff spinner1
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
OnItemSelectedListener onItemSelectedListener2 = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do your stuff spinner2
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
}
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