0

So what I'm trying to do is to refresh my webView using a button in the actionbar, the problem? Well, it is easier to explain with some code.

This is at the bottom of my MainActivity.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

final String url=getArguments().getString("url");

View rootView = inflater.inflate(R.layout.fragment_main_dummy,container, false);
WebView wv = (WebView)rootView.findViewById(R.id.webView);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl("http://feedit.themeister.se/app/"+url+".php");
wv.setWebViewClient(new WebViewClient());
return rootView;
}

And in order to refresh the webView I need the "url" string but the string is created inside the onCreateView, above that I have this one to actually make the refresh-button work

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
    case R.id.action_refresh:
        // Here should some code be placed but I don't know what to place
        return true;
    default:
        return super.onOptionsItemSelected(item);
}

}

I can't use the url string or the "wv" for webView. Anyone know how I should make this work?

Thanks in advance!

TheMeisterSE
  • 541
  • 1
  • 7
  • 28
  • you need to call again `wv.loadUrl("http://feedit.themeister.se/app/"+url+".php");` – Deepak Swami Sep 26 '13 at 18:19
  • possible duplicate of [Is there a better way to refresh WebView?](http://stackoverflow.com/questions/2563325/is-there-a-better-way-to-refresh-webview) – Deepak Swami Sep 26 '13 at 18:21

1 Answers1

1

I can't use the url string or the "wv" for webView. Anyone know how I should make this work?

This is because your two variables, the url and wv are locals, meaning that they only exists under your onCreateView method.

To use your variables in the onOptionsItemSelected method, put their declaration outside of their method, like in top of the class.

So, in steps :

  • put WebView wv; on top of the class,
  • Change WebView wv = (WebView)rootView.findViewById(R.id.webView); in wv = (WebView)rootView.findViewById(R.id.webView);
  • Modify your onOptionsItemSelected to refresh the webview like this :

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_refresh:
            wv.loadUrl(wv.getUrl());
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
    

I also suggest you to reed the basics of Java about variable scopes, (like this one : http://www.java-made-easy.com/variable-scope.html) and look for other tutorials about java basics before going on Android code.

meynety
  • 561
  • 3
  • 6
  • There are two problems. One new problem and one existing. 1. When putting the `wv = (WebView)rootView.findViewById(R.id.webView);` inside onOptionsItemSelected I get an error saying "rootView cannot be resolved" and just adding the line I have below in onCreateView doesn't work because I don't have the inflater and such. 2. Still cannot use "url". – TheMeisterSE Sep 26 '13 at 20:04
  • 1. No no, you misunderstood, you don't have to change the position of `wv = (WebView)rootView.findViewById(R.id.webView);`, just to remove the `WebView` at start of the line. 2. You don't have to use the variable `url` in the `onOptionsItemSelected`, just use `wv.loadUrl(wv.getUrl());`. – meynety Sep 26 '13 at 20:26