How to load HTML file in webview on onitemclick

后端 未结 2 1498
暖寄归人
暖寄归人 2021-01-25 10:59

i have more than 100 html files and i want each file to open on row click in listview and each html file should open in the webview , i tried this code but this is not working,

2条回答
  •  旧巷少年郎
    2021-01-25 11:08

    For your purpose use this

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

    Here "description" is name of string containing data with or without html tags

     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);
    
          }
         }); 
    

    If you need to render url then try

    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);
                }
    
            }
    
    
    
    
    }
    

提交回复
热议问题