Adding OnItemSelectedListener to Spinner - android

I have a button and a spinner (originally hidden). When user presses a button, spinner gets populated with items and becomes visible. Now I would like to add OnItemSelectedListener to the spinner. and I have tried many tutorials with no luck.
This is my OnCreate function
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button load_routes = (Button)findViewById(R.id.load_routes);
Spinner routes = (Spinner)findViewById(R.id.routes_list);
load_routes.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
load_routes(v);
}
});
routes.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View v, int position, long id)
{
Log.v("routes", "route selected");
}
public void onNothingSelected(AdapterView<?> arg0)
{
Log.v("routes", "nothing selected");
}
});
}
This is my load_routes function
private void load_routes(View v)
{
Spinner routes = (Spinner)findViewById(R.id.routes_list);
List<String> routes_list = RouteParser.get_routes();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, routes_list);
routes.setAdapter(adapter);
TableRow list_of_routes_row = (TableRow)findViewById(R.id.list_of_routes_row);
list_of_routes_row.setVisibility(View.VISIBLE);
}
This set up does not work. The only way I got this to work is when I setup my listener as routes.setOnItemSelectedListener(this) Then I implement OnItemSelectedListener and include the functions neccessary. But I have multiple spinners and need to create separate listeners for different spinner. Any help will be appreciated. Thanks!

final String[] s2 = getResources().getStringArray(R.array.capteur_size);
final EditText ed = (EditText) findViewById(R.id.editTextCoC);
spinnerCoC = (Spinner) findViewById(R.id.spinnerCoC);
spinnerCoC.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
ed.setText(s2[arg2]);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

Declare your Spinner as field instantiate the listener once you do findViewById and use it wherever you want.

Related

How to display value of two Spinner(present in one activity) in a TextView: Android

I am unable to display the value of selected spinner in the TextView.
None of the spinner value is being displayed.
Is there any way to display values of both the spinners?
here is the code
public class Calculator extends Activity implements OnItemSelectedListener {
Spinner temp1,temp2;
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
t1=(TextView)findViewById(R.id.t1);
temp1=(Spinner)findViewById(R.id.temp);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this,R.array.temperature,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
temp1.setAdapter(adapter);
temp2=(Spinner)findViewById(R.id.temp2);
ArrayAdapter<CharSequence> adapter2=ArrayAdapter.createFromResource(this,R.array.temperature,android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
temp2.setAdapter(adapter2);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner temp1=(Spinner)parent;
Spinner temp2=(Spinner)parent;
if (temp1.getId()==R.id.temp) {
String item = parent.getItemAtPosition(position).toString();
t1.setText(item);
}
if (temp2.getId()==R.id.temp2) {
String item = parent.getItemAtPosition(position).toString();
t1.setText(item);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
You did not added listener to your spinners.
Use:
temp1.setOnItemSelectedListener(this);
temp2.setOnItemSelectedListener(this);

How to maintain spinner selected state in android

I just wanna maintain spinner selected state in android.I've an activity A which retrieves data from webservice & popped into spinner.After select any one item from that and goes to another activity B.Once back to activity A.,i need to show selected item on spinner without go to web call again.
My Code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
spinner = (Spinner) findViewById(R.id.spinner);
if(spinnerFlag=1){
spinner.setSelection(index);
}
else{
//Web call for spinner data
}
...
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View view, int arg2, long arg3) {
String seletcedProductName = parent.getSelectedItem()
.toString();
spinnerFlag=0;
index=arg2;
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Thanks.
Just save the index in SharedPreferences on activity stop, retrieve it when you return to that activity.
Shared Preferences at Android Developer
You can save selected index of spinner in static variable, initialized with -1, and onResume check the value of that variable, if it's not -1, then set the selected index of spinner with it.
static int position=-1;
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View view, int arg2, long arg3) {
String seletcedProductName = parent.getSelectedItem()
.toString();
spinnerFlag=0;
index=arg2;
position=arg2;
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
#Override
protected void onResume() {
super.onResume();
if(position!=-){
spinner.setSelection(index);
}
}

How do i go to the next screen by selecting item (just tapping on the item) from spinner in android?

I am trying to get a screen transition when i select a value from the spinner. My spinner has just 2 values. The 1st one is selected by default. What I want is that when i click on the 2nd value in my spinner, it should take me to the new screen.
Please Help!
Thanks in advance!
Use onItemSelectedListener of your Spinner. Here is a Demo,
public class AndroidSpinner extends Activity implements OnItemSelectedListener {
TextView selection;
Spinner spin;
String[] items = { "bangladesh", "bangla", "bd", "australia", "japan",
"china", "indiaA", "indiaC" };
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Spinner spin = new Spinner(this);
setContentView(spin);
spin.setOnItemSelectedListener(this);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, items);
spin.setAdapter(aa);
}
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// Do your Stuff Here
Intent intent = new Intent(MyActivity.this, NextActivity.class);
startActivity(intent);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
selection.setText("");
}
}
Assuming that you know how to set up the spinner adapter and everything, simply add the code to go to the next screen when the other item is selected on the spinner.
public void onItemSelected(AdapterView<?> parent, View v, int position, long id)
{
if(position ==0)
//do nothing (assuming that you would want to stay in the same screen)
else if (position ==1)
{
//go to the next screen, probably by using an INTENT to the next activity or using setContentView(theLayoutXmlFile)
}
}
Use below code for open new activity or new screen onitemselected event of spinner.
public class SpinnerExample extends Activity implements OnItemSelectedListener {
String[] items = { "Dipak", "Aadi", "Bharat", "Pratik", "Usha", "Jayesh", "Deep", "Imran" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mSpn1 = (Spinner) findViewById(R.id.mSpn1);
mSpn1.setOnItemSelectedListener(this);
ArrayAdapter<String> adpt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
mSpn1.setAdapter(adpt);
}
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
// Do your Stuff Here
// For Open New Activity
Intent mInNewAccount = new Intent(MainActivity.this, SecondActivity.class);
startActivity(mInNewAccount);
finish();
// For Open New Screen
setContentView(R.layout.second_screen);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}

Spinner setOnItemSelectedListener Doesn't Fire

I have a view object that contains a edittext, an invisible spinner, and a button. I load the view via the LayoutInflater and it shows up and everything works fine. I hit the button, the spinner list shows up and I select the item that I want. My problem is that the setOnItemSelectedListener for the spinner doesn't fire so I can't set the edittext to the selected value.
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, listItems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spn = (Spinner)sEdit.findViewWithTag("spn"+fieldID);
final String spnHolder = "spn"+fieldID;
spn.setAdapter(adapter);
ImageButton bSpn = (ImageButton)sEdit.findViewWithTag("btn"+fieldID);
bSpn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
curVw = (EditText)sEdit.findViewWithTag(v.getTag().toString().split("btn")[1]);
((Spinner)sEdit.findViewWithTag(spnHolder)).performClick();
}
});
try {
spn.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
curVw.setText(((TextView)arg1).getText().toString());
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
catch (Exception ex)
{
ex.printStackTrace();
}
I added a Try...Catch to see if it was just failing past that point, but it is never called.
Instead of using SetOnclickListener please use:
spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
//implements methods and perform your action. This will works definately
};

Related spinners Android

I'm begginer in android. I'm working on a project. But i get very difficult to do two spinners related to each others. Actually one spinner for the country and another for the city. Instead of the country that is chosen the second spinner will show the cities.
I'v used "OnItemSelectedListener" but the " ArrayAdapter.createFromResourc​e " can't be used inside OnItemSelectedListener.
I've tried a lot of other ways but still none of them working.
Can anybody help me Please???
(P.S. I have read and tried the other posts about this topic but it still doesn't work )
This is the code:
spinner.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
int spinnerId = spinner.getSelectedItemPosition();
if (spinnerId==0){
adaptert = ArrayAdapter.createFromResource(
this, R.array.tirana, android.R.layout.simple_spinner_item);
adaptert.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
else if (spinnerId==1) {
adaptert = ArrayAdapter.createFromResource(
this, R.array.durres, android.R.layout.simple_spinner_item);
adaptert.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
spinnert.setAdapter(adaptert);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
public class AdapterViewImplementation extends Activity implements OnItemSelectedListener{
Spinner sp1; // One Spinner
Spinner sp2; // Another Spinner
ArrayAdapter stateAdapter; // Adapter for state
ArrayAdapter cityAdapter; // Adapter for city
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sp1 = (Spinner)findViewById(R.id.Spinner01);
sp2 = (Spinner)findViewById(R.id.Spinner02);
stateAdapter = ArrayAdapter.createFromResource(AdapterViewImplementation.this,
R.array.state, android.R.layout.simple_spinner_item);
stateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(stateAdapter);
sp1.setOnItemSelectedListener(AdapterViewImplementation.this);
cityAdapter = ArrayAdapter.createFromResource(AdapterViewImplementation.this,
R.array.city, android.R.layout.simple_spinner_item);
cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(cityAdapter);
sp2.setOnItemSelectedListener(AdapterViewImplementation.this);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if(arg0 == sp1){
sp2.setSelection(arg2);
}else{
sp1.setSelection(arg2);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}

Categories

Resources