Problem with Spinner onClickListener


Last Updated:

  1. AshtrayY

    AshtrayY New Member This Topic's Starter

    Joined:
    Mar 21, 2010
    Messages:
    1
    Likes Received:
    0
    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 (Text):
    1. package m.foods;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.webkit.WebView;
    7. import android.widget.ArrayAdapter;
    8. import android.widget.Button;
    9. import android.widget.Spinner;
    10. import android.widget.TextView;
    11.  
    12. public class Foods extends Activity {
    13.     Spinner    localSpinner;
    14.     TextView   text1;
    15.     Button     button1;
    16.     WebView    webview;
    17.    
    18.     /** Called when the activity is first created. */
    19.     @Override
    20.     public void onCreate(Bundle savedInstanceState) {
    21.         super.onCreate(savedInstanceState);
    22.         setContentView(R.layout.main);
    23.                                                    
    24.         localSpinner = (Spinner)findViewById(R.id.localSpinner);
    25.         text1 = (TextView)findViewById(R.id.text1);
    26.         button1 = (Button)findViewById(R.id.button1);
    27.         webview = (WebView)findViewById(R.id.webview1);
    28.        
    29.         ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
    30.                     R.array.restaurants, R.layout.my_normal_spinner_item_style);
    31.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    32.         localSpinner.setAdapter(adapter);
    33.      
    34.         button1.setOnClickListener(new clicker());
    35.     }
    36.  
    37.     class  clicker implements  Button.OnClickListener
    38.     {
    39.         public void onClick(View v) {
    40.             String s = localSpinner.getSelectedItem().toString();
    41.             text1.setText(s);
    42.             if(s == "Chili &amp; Lime")
    43.               webview.loadUrl("http://a.se");
    44.             else if(s == "JB")
    45.               webview.loadUrl("http://b.se");
    46.             else
    47.               webview.loadUrl("http://c.se");  
    48.         }
    49.     }
    50. }
    51.  
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <resources>
    3.     <string name="app_name">Mj
     

    Advertisement
  2. disco6stu9

    disco6stu9 Member

    Joined:
    Apr 25, 2010
    Messages:
    10
    Likes Received:
    7
    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
     
  3. swindelljd

    swindelljd Well-Known Member

    Joined:
    Apr 2, 2010
    Messages:
    63
    Likes Received:
    5
    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 (Text):
    1.  
    2. spinner.setSelection(setSpinnerIndex(getResources().getStringArray(R.array.testArray),
    3.                     mCursor.getString(1)));
    4.  
    Code (Text):
    1.  
    2.     protected int setSpinnerIndex(String[] lookup, String checkStr)
    3.     { int returnVal = 0;
    4.         if (lookup != null && checkStr != null)
    5.         {
    6.             for (int i = 0;i < lookup.length; i++)
    7.             {
    8.                 if (lookup[i].equals(checkStr))
    9.                 {
    10.                     returnVal = i;
    11.                     break;
    12.                 }
    13.             }
    14.         }
    15.         return returnVal;
    16.     }
    17.  
     

Share This Page

Loading...