2012-01-24 55 views
99

嗨我解析XML然后将其加载到Web视图,解析后我创建四个字符串,以便我可以将所有字符串追加到一个视图。我能够在Web视图中获得两个视图,但不能获得前两个字符串。如何将html字符串传递给android上的webview

PLS建议我用我的代码,我该怎么错在何处,什么是让在网络上查看的HTML格式的字符串的正确方法。请看看我的代码,并帮助我解决这个问题。

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

我能够在web视图上获得chapterIntro和chaptercontent,但不是前两个字符串请帮助我的朋友。

回答

115

在WebView中加载数据。调用loadData的WebView

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

()方法可以检查这个例子

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

+0

我与尝试,仍然没有发生还与loadbaserul – cavallo

+0

尝试的是,应该用包含JavaScript字符串工作吗?它不适用于我 – Edu

+22

它应该是'webView.loadData(yourData,“text/html; charset = utf-8”,“UTF-8”);' – Jaroslav

137

我已经成功地通过下面的线

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

你把这个添加到index.php/index.html上的? –

+1

亚,如果你想,否则它会好的 –

+0

它稍微延迟后更新UI。如何解决这个问题? – NarendraJi

18

传递null会更好做。完整的代码是这样的:

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

你这是在index.php/index.html的 –

+0

@mthethelelibeseti添加到这是Android代码,而不是HTML。或者我误解了你的问题? –

相关问题