如何在onitemclick上加载webview中的HTML文件

时间:2013-12-04 12:29:55

标签: android html webview

我有超过100个html文件,我希望每个文件在列表视图中的行点击打开,每个html文件应该在webview中打开,我尝试了这个代码,但这不起作用,只有webview打开行点击, html文件没有显示出来。

 package com.Example.apk;



import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebViewClient;

public class WebView extends Activity {

public class WebViewActivity extends Activity {
    WebView web;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        web = (WebView) findViewById(R.id.webview1);
        web.setWebViewClient(new myWebClient());
        web.getSettings().setJavaScriptEnabled(true);
        int pos = getIntent().getIntExtra("key",0);
        if(pos==0)
        {
        web.loadUrl("file:///android_asset/work.html");
        }
        else if(pos==1)
        {
        web.loadUrl("file:///android_asset/work1.html");
        }  
        else if(pos==2)
        {
        web.loadUrl("file:///android_asset/work2.html");
        } 
        else if(pos==3)
        {
        web.loadUrl("file:///android_asset/work3.html");
        }  
      // similarly for 4 and 5 and so on.
    }

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            view.loadUrl(url);
            return true;

        }
        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

        }
    }
}
}

2 个答案:

答案 0 :(得分:1)

将位置传递给下一个活动

list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
Intent myIntent = new Intent(MainActivtiy.this,WebViewActivity.class);
myIntent.putExtra("key",position);
startActivity(myIntent);
}
});   

然后在WebViewActivity中,即activity_webview)

public class WebViewActivity extends Activity {
WebView web;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);
    web = (WebView) findViewById(R.id.webview1);
    web.setWebViewClient(new myWebClient());
    web.getSettings().setJavaScriptEnabled(true);
    int pos = getIntent().getIntExtra("key",0);
    if(pos==0)
    {
    web.loadUrl("file:///android_asset/work.html");
    }
    else if(pos==1)
    {
    web.loadUrl("file:///android_asset/work1.html");
    }  
    else if(pos==2)
    {
    web.loadUrl("file:///android_asset/work2.html");
    } 
    else if(pos==3)
    {
    web.loadUrl("file:///android_asset/work3.html");
    }  
   ...// similarly for 4 and 5 and so on.
}

public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub

        view.loadUrl(url);
        return true;

    }
    @Override
    public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
    }
    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);

    }
}

WebViewActivity的xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

在Manifest文件中为WebViewActivity创建一个条目。

答案 1 :(得分:0)

出于您的目的使用此

WebView wv = (WebView)rootView.findViewById(R.id.go_web_view);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.loadDataWithBaseURL(null, description, "text/html", "utf-8", null);

此处“description”是包含带或不带html标签的数据的字符串的名称

 list1.setOnItemClickListener(new OnItemClickListener() {
     public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
    String url = m_ArrayList.get(arg2).your_url;
                   Intent myIntent = new     Intent(MainActivtiy.this,AppWebView.class);
                   myIntent.putExtra("key",url);
                   startActivity(myIntent);

      }
     }); 

如果您需要渲染网址,请尝试

public class AppWebView extends Activity{

    WebView webView;
    ProgressBar pBar;

    @SuppressLint("SetJavaScriptEnabled")
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web_view);
        pBar = (ProgressBar)findViewById(R.id.progressBar1);

        String newUrl;
        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            if (extras == null) {
                newUrl = null;
            } else {
                newUrl = extras.getString("url");
            }
        } else {
            newUrl = (String) savedInstanceState
                    .getSerializable("myJsonStringS");
        }

        Log.d("jitendra", newUrl);

        //SharedPreferences sp = getSharedPreferences("booking_detail", 0);
        //String jsonString = sp.getString("jsonString", "");

        webView = (WebView)findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new myWebClient());
        webView.loadUrl(newUrl);

    }
    public void moveToThanksPage()
    {
        Intent intent = new Intent(this,ThankYou.class);
        startActivity(intent);
    }
     public class myWebClient extends WebViewClient
        {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                Log.d("sagarWeb", url);
                if (url.startsWith("mailto:")) {
                    String[] blah_email = url.split(":");
                    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                    emailIntent.setType("text/plain");
                    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{blah_email[1]});
                 //   emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "what_ever_you_want_the_subject_to)");
                    Log.d("NOTICE", "Sending Email to: " + blah_email[1] + " with subject: " + "what_ever_you_want_the_subject_to_be");
                    startActivity(emailIntent);
                }
                else if (url.startsWith("tel:")) {
                    Log.d("Web", "tell");
                    String uri = url;
                    Intent intent = new Intent(Intent.ACTION_CALL);
                    intent.setData(Uri.parse(uri));
                    startActivity(intent);
                }
                else if (url.endsWith("error.jsp")) {
                    Log.d("Web", "Error");
                }
                /*else if (url.contains("thankyou/app")) {

     //===================== USE UNDERMENTIONED COMMENT ON FOR SELF THANKS PAGE ==================//

                    //moveToThanksPage(); 
                }*/
                else
                {
                    view.loadUrl(url);
                    pBar.setVisibility(View.VISIBLE);
                }
                return true;

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                pBar.setVisibility(View.GONE);
                super.onPageFinished(view, url);

                //progressBar.setVisibility(View.GONE);
            }

        }




}