How to load HTML file in webview on onitemclick

后端 未结 2 1492
暖寄归人
暖寄归人 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:04

    Pass the position to next Activity

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

    Then in WebViewActivity ie 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);
    
        }
    }
    

    xml for WebViewActivity

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

    Make an entry for WebViewActivity in Manifest file.

    0 讨论(0)
  • 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);
                }
    
            }
    
    
    
    
    }
    
    0 讨论(0)
提交回复
热议问题