Go Back   Android Forums > Android Development > Application Development
Application Development Dev Lounge for the Coder Folks
Gamers - Check out our new sister sites!
Nintendo Wii U!    |    OUYA - $99 Android System!

test: Reply
 
LinkBack Thread Tools
Old March 23rd, 2010, 01:26 PM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: Mar 2010
Posts: 1
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with Spinner onClickListener

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ärdeviFoods</string>
    <string-array name="restaurants">
      <item name="chili">Chili &amp; Lime</item>
      <item name="collegium">Collegium</item>
      <item name="golfinn">Golf Inn</item>
      <item name="husman">Husman</item>
      <item name="jb">JB</item>
      <item name="brodernas">Brödernas</item>
    </string-array>
</resources>
Most of all I would like the menu for the selected restaurant to be displayed as soon as the user choses a restaurant from the Spinner, but since I couldn't get that to work, I added a OK button, but I can't seem to get that to work either, it always displays webpage c.se.

Could anyone please tell me what I'm doing wrong?

/Ash

AshtrayY is offline  
Reply With Quote
Sponsors
Old April 26th, 2010, 12:08 AM   #2 (permalink)
New Member
 
Join Date: Apr 2010
Location: Seattle, WA
Posts: 10
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 7 Times in 2 Posts
Default

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%28andro id.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
disco6stu9 is offline  
Reply With Quote
Old April 26th, 2010, 11:06 PM   #3 (permalink)
Junior Member
 
Join Date: Apr 2010
Posts: 60
 
Device(s):
Carrier: Not Provided

Thanks: 6
Thanked 5 Times in 4 Posts
Default

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;
    }
swindelljd is offline  
Last edited by swindelljd; April 26th, 2010 at 11:08 PM.
Reply With Quote
Reply


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 03:25 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.