I have two spinners (day_spin and time_spin) in one Activity
. I want to save the selected day_spin value into a variable. Is it possible to differenciate betwee
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);
//Spinner spnir = (Spinner)parent;
parent.getItemAtPosition(pos);
TextView tvReadValue = (TextView) findViewById(R.id.tvReadValue);
switch (parent.getId())
{
case R.id.spinDimension:
tvReadValue.setText("spinDimension" + pos);
break;
case R.id.speedDimension:
tvReadValue.setText("speedDimension" + pos);
break;
}
in onItemSelected
method
int i = spinnerFirst.getSelectedItemPosition();
int j = spinnerSecond.getSelectedItemPosition();
String strName1 = yourFirstarray[i];
String strName2 = yourSecondarray[j];
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (adapterView.getId() == R.id.number_of_poket_spinner) {
NumberOfPokets item = (NumberOfPokets) adapterView.getSelectedItem();
Toast.makeText(this, item.getPokets(), Toast.LENGTH_LONG).show();
} else if (adapterView.getId() == R.id.select_pleats) {
PleatsModel item2 = (PleatsModel) adapterView.getSelectedItem();
Toast.makeText(this, item2.getPleats(), Toast.LENGTH_LONG).show();
}
}
try this...
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if(parent.getId() == R.id.spinnerone)
{
// operations
}
else if(parent.getId() == R.id.spinnertwo)
{
//operations
}
}
Two Spinner in same class
I try a lot of this but at the last, I got a working Code.
state = findViewById(R.id.spinnerState);
country = findViewById(R.id.spinnerCountry);
List<String> categories = new ArrayList<String>();
categories.add("Select State");
categories.add("Andhra Pradesh");
categories.add("Arunachal Pradesh");
categories.add("Assam");
categories.add("Bihar");
categories.add("Chandigarh");
categories.add("Delhi");
categories.add("Goa");
categories.add("Gujarat");
categories.add("Haryana");
categories.add("Himachal Pradesh");
categories.add("Jammu and Kashmir union territory");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SaveUser.this, android.R.layout.simple_spinner_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
state.setAdapter(dataAdapter);
state.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), item, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// can leave this empty
}
});
List<String> categories2 = new ArrayList<String>();
categories2.add("Select Country");
categories2.add("Afghanistan");
categories2.add("Albania");
categories2.add("Algeria");
categories2.add("India");
categories2.add("Andorra");
categories2.add("Angola");
categories2.add("Antigua and Barbuda");
categories2.add("Argentina");
categories2.add("Armenia");
// Div Spinner implementing onItemSelectedListener
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(SaveUser.this, android.R.layout.simple_spinner_item, categories2);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
country.setAdapter(dataAdapter2);
country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), item, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// can leave this empty
}
});
}
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if(parent.getId() == R.id.spinner1)
{
//do this
}
else if(parent.getId() == R.id.spinner2)
{
//do this
}
}