2012-01-24 9 views
99

こんにちは私はxmlを解析して、Webビューに読み込んだ後、解析した後、すべての文字列を1つのビューに追加できるように4つの文字列を作成しています。最初の2つの文字列ではなく、Webビューで2つのビューを取得できます。androidのwebviewにhtml文字列を渡す方法

Plsは私のコードで私に間違っていると私は示唆し、どのような正しい方法は、Webビュー上でフォーマットされたHTML文字列を取得することです。 Plsは私のコードを見て、私がこの問題を解決するのを助けます。

@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を得ることではなく、最初の二つの文字列のPLSのは私の友人を助けることができています。

答えて

115

WebViewでデータを読み込む。私が正常に以下の行で

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

は、私はそれと試みたが、まだもloadbaserul – cavallo

+0

で試してみましたが起こっていないというのjavascriptを含む文字列で動作するようになっていますか?それは私のために働いていない – Edu

+22

'webView.loadData(yourData、" text/html; charset = utf-8 "、" UTF-8 ");' – Jaroslav

137

を確認することができますWebViewの

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

のloaddataの()メソッドを呼び出して渡す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のに追加しますか? –

+1

あなたがしたいのであれば、そうでなければOKです –

+0

ちょっと遅れてUIを更新します。そのことをどのように修正するのですか? – NarendraJi

関連する問題