• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problem with Spinner onClickListener

AshtrayY

Lurker
Hi,

I'm trying to make a small app showing some web pages with lunch menus from different restaurants. The thought is to use a Spinner to choose which restaurant your interested in and then todays menu, as a webpage, is shown below the spinner. However, I can't seem to manage to read from the Spinner which restaurant is chosen.

Here is my code.

Code:
package m.foods;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class Foods extends Activity {
	Spinner    localSpinner;
	TextView   text1;
	Button     button1;
	WebView    webview;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.main);
    	                                            
    	localSpinner = (Spinner)findViewById(R.id.localSpinner);
    	text1 = (TextView)findViewById(R.id.text1);
    	button1 = (Button)findViewById(R.id.button1);
    	webview = (WebView)findViewById(R.id.webview1);
    	
    	ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
    	            R.array.restaurants, R.layout.my_normal_spinner_item_style);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        localSpinner.setAdapter(adapter);
     
        button1.setOnClickListener(new clicker());
    }

    class  clicker implements  Button.OnClickListener 
    {
        public void onClick(View v) {
        	String s = localSpinner.getSelectedItem().toString();
        	text1.setText(s);
        	if(s == "Chili &amp; Lime")
        	  webview.loadUrl("http://a.se");
        	else if(s == "JB")
        	  webview.loadUrl("http://b.se");
        	else
        	  webview.loadUrl("http://c.se");	
        }
    }
}

Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Mj
 
You may want to try implementing AdapterView.OnItemSelectedListener (AdapterView.OnItemSelectedListener | Android Developers) instead of OnClickListener. Then you can pass your implementation to the Spinner class using localSpinner.setOnItemSelectedListener (http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemSelectedListener%28android.widget.AdapterView.OnItemSelectedListener%29)

When the user picks a value from the spinner the position within the list of options will be passed to the onItemSelected method of your listener. You could use this value to figure out what is picked or try to inspect the return value of getSelectedItem() like your code above.

Right now since nothing is selected when you click the button your call always falls through to the last else condition.

FWIW, I've used spinners in my apps and found this method to work. I followed the example on the Android developer site:

Binding to Data with AdapterView | Android Developers
 
And if you are trying to set your spinner index from a stored database value you could use this utility that I created. The String[] is from an <array>...</array> and the checkStr is the value from the db. I call it like this
Code:
spinner.setSelection(setSpinnerIndex(getResources().getStringArray(R.array.testArray),
                    mCursor.getString(1)));
Code:
    protected int setSpinnerIndex(String[] lookup, String checkStr)
    { int returnVal = 0;
        if (lookup != null && checkStr != null)
        {
            for (int i = 0;i < lookup.length; i++)
            {
                if (lookup[i].equals(checkStr))
                {
                    returnVal = i;
                    break;
                }
            }
        }
        return returnVal;
    }
 
Back
Top Bottom