2

I am trying to make a timetable app for my school and I want to use a spinner with all classes in it. When you select your class I want it to open a specific URL with the timetable in it. I tried so many ways but it's just not working.. I prefer not to use a "submit" button with it.. can somebody help me?

Edit: think I figured it out, but here's my next problem: I want it to open a website with a if statement but when I try this it gives me an error: The method findViewById(int) is undefined for the type CustomOnItemSelectedListener

public class CustomOnItemSelectedListener implements OnItemSelectedListener {


public void onItemSelected(AdapterView<?> parent, View view, 
        int pos, long id) { 
if (parent.getItemAtPosition(pos) == "h5c") {
WebView myWebView = (WebView) findViewById(R.id.webView1);
}

}


public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
}

}

Really hope u guys understand, since I am a noob at this :p

  • 2
    what have you tried? can you edit your question and include parts of your codebase that would help anyone answer this correctly. if you are looking some help to get started see this : http://developer.android.com/guide/topics/ui/controls/spinner.html – petey Sep 05 '13 at 17:22
  • figured it out, but I got an error now.. thanks for your help btw – Floris ten Hacken Sep 05 '13 at 18:30
  • can you add your activity's layout file? – petey Sep 05 '13 at 19:31

1 Answers1

1

Try this out. This is the complete answer to your query.

Also give internet permissions in AndroidManifest

private WebView wv1;
String url;

Spinner spinner;
private String[] spinner_and_webview = {"Learn Spinner", "Learn WebView"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<String> adapter0 =
            new ArrayAdapter<String>(MainActivity.this,
                    android.R.layout.simple_spinner_item, spinner_and_webview);
    adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter0);
    spinner.setOnItemSelectedListener(onItemSelectedListener0);

    wv1 = (WebView) findViewById(R.id.webView);
    wv1.setWebViewClient(new MyBrowser());
}

AdapterView.OnItemSelectedListener onItemSelectedListener0 =
        new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {

                String sp1 = String.valueOf(spinner.getSelectedItem());
                if (sp1.contentEquals("Learn Spinner")) 
                    url = "http://developer.android.com/guide/topics/ui/controls/spinner.html";
                if (sp1.contentEquals("Learn WebView")) 
                    url = "http://developer.android.com/reference/android/webkit/WebView.html";
                wv1.getSettings().setLoadsImagesAutomatically(true);
                wv1.getSettings().setJavaScriptEnabled(true);
                wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
                wv1.loadUrl(url);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        };
private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}
Gorav Sharma
  • 410
  • 5
  • 7