2012-01-24 3 views
99

Xin chào Tôi đang phân tích cú pháp xml và sau đó tải nó vào chế độ xem web, sau khi phân tích cú pháp, tôi đang tạo bốn chuỗi để tôi có thể nối tất cả chuỗi vào một chế độ xem. Tôi có thể nhận được hai chế độ xem trên chế độ xem web nhưng không thể xem hai chuỗi đầu tiên.Làm cách nào để chuyển chuỗi html vào webview trên android

Xin giới thiệu cho tôi mã của tôi, tôi đang đi sai ở đâu và cách chính xác để nhận các chuỗi html được định dạng trên chế độ xem web là gì. Xin vui lòng xem mã của tôi và giúp tôi giải quyết vấn đề này.

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     String chapterTitle = ""; 
     String SubChapterTitle=""; 
     String chapterIntro =""; 
     String chapterContent=""; 
     View view = convertView; 
     if (convertView == null) { 
      // view = inflater.inflate(resourceid, null); 
      view = getLayoutInflater().inflate(R.layout.webviewitem, null); 
     } 
     synchronized (view) { 
      WebView wv = (WebView) view.findViewById(R.id.contentWebView); 

      WebSettings settings = wv.getSettings(); 
      settings.setUseWideViewPort(true); 
      settings.setLoadWithOverviewMode(true); 
      settings.setJavaScriptEnabled(true); 
      settings.setDefaultZoom(ZoomDensity.FAR); 
      // wv.setBackgroundColor(0); 
      wv.setVerticalScrollBarEnabled(false); 
      wv.setHorizontalScrollBarEnabled(false); 
      /*String txtChapTitle = Intro.book.getsecretList().get(position) 
        .getChtitle().toString();*/ 

      if (!(Intro.book.getsecretList().get(position).getChtitle() 
        .toString().equals(""))){ 
      chapterTitle = "<b><fontSize=4>"+Intro.book.getsecretList().get(position) 
      .getChtitle().toString()+"</font></b>"; 
      } 
      if (!(Intro.book.getsecretList().get(position) 
        .getSubtitle() == null)) { 
       SubChapterTitle = "<b><fontSize=4>"+Intro.book.getsecretList().get(position) 
       .getSubtitle().toString()+"</font></b>"; 
      } 
      if (!(Intro.book.getsecretList().get(position) 
        .getIntro() == null)) { 
      chapterIntro = "<b><fontSize=2>"+Intro.book.getsecretList().get(position) 
       .getIntro().toString()+"</font></b>"; 
      } 
      if (!(Intro.book.getsecretList().get(position) 
        .getContent() == null)) { 
      chapterContent = "<fontSize=2>"+Intro.book.getsecretList().get(position) 
       .getContent().toString()+"</font>"; 
      } 

      StringBuilder content = new StringBuilder(); 
      content.append(chapterTitle+SubChapterTitle+chapterIntro+chapterContent); 

      JsInterface Jsi = new JsInterface(); 
      Jsi.wordDef = content ; 
      Log.v("Content", "" +content); 
      wv.addJavascriptInterface(Jsi, "interfaces"); 

      wv.setWebViewClient(new WebViewClient() { 
       @Override 
       public void onPageFinished(WebView view, String url) { 
        view.setHapticFeedbackEnabled(false); 
       } 
      }); 

      wv.setWebChromeClient(new WebChromeClient() { 
       @Override 
       public boolean onJsAlert(WebView view, String url, 
         String message, JsResult result) { 
        return super.onJsAlert(view, url, message, result); 
       } 
      }); 

      wv.loadUrl("file:///android_asset/wordview.html"); 
     } 
     return view; 
    } 
} 

Tôi có thể lấy chapterIntro và chaptercontent trên chế độ xem web nhưng không phải là hai chuỗi đầu tiên giúp bạn bè.

Trả lời

115

Để tải dữ liệu của bạn trong WebView. Gọi loadData() phương pháp của WebView

webView.loadData(yourData, "text/html; charset=utf-8", "UTF-8"); 

Bạn có thể kiểm tra ví dụ này

http://developer.android.com/reference/android/webkit/WebView.html

+0

tôi đã cố gắng với điều đó, vẫn không xảy ra cũng đã cố gắng với loadbaserul – cavallo

+0

là nghĩa vụ phải làm việc với một chuỗi chứa javascript? Nó không hoạt động cho tôi – Edu

+22

nó phải là 'webView.loadData (yourData," văn bản/html; charset = utf-8 "," UTF-8 ");" – Jaroslav

137

tôi đã thực hiện thành công bởi dòng dưới đây

//data == html data which you want to load 
WebView webview = (WebView)this.findViewById(R.id.webview); 
webview.getSettings().setJavaScriptEnabled(true); 
webview.loadDataWithBaseURL("", data, "text/html", "UTF-8", ""); 
+0

bạn có thêm mã này vào số trên index.php/index.html không? –

+1

ya nếu bạn muốn, nếu không nó sẽ là ok –

+0

Nó cập nhật giao diện người dùng sau một chút chậm trễ. Làm thế nào để sửa chữa điều đó? – NarendraJi

18

Passing rỗng sẽ tốt hơn. Các mã đầy đủ như:

WebView wv = (WebView)this.findViewById(R.id.myWebView); 
wv.getSettings().setJavaScriptEnabled(true); 
wv.loadDataWithBaseURL(null, "<html>...</html>", "text/html", "utf-8", null); 
+0

bạn có thêm mã này vào số trên index.php/index.html? –

+0

@mthethelelibeseti đây là mã Android, không phải HTML hoặc tôi hiểu nhầm câu hỏi của bạn? –