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
|
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 & 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 & 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
|
|
|