2012-01-24 4 views
99

안녕하세요. xml을 구문 분석하고 웹 뷰에로드하는 중 구문 분석 후 하나의보기에 모든 문자열을 추가 할 수 있도록 4 개의 문자열을 만듭니다. 웹보기에서 두 개의보기를 얻을 수 있지만 처음 두 개의 문자열은 가져올 수 없습니다.android에서 webview에 HTML 문자열을 전달하는 방법

Pls에서 코드를 제안합니다. 어디서 잘못 가고 있으며, 웹보기에서 형식이 지정된 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; 
    } 
} 

나는 웹보기에 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

과 노력 발생하지 않는 것이 자바 스크립트를 포함한 캐릭터와 함께 작동하도록되어? 그것은 작동하지 않습니다 – Edu

+22

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

137

을 확인할 수 있습니다 웹보기

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

의() 메소드 loadData를 호출합니다. 같이 전체 코드입니다 :

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

나중에 원한다면, 그렇지 않으면 괜찮습니다 –

+0

약간의 지체 후에 UI를 업데이트합니다. 그걸 고치는 법? – NarendraJi

18

합격 널 (null)이 더 좋을 것이다 아래 라인으로 수행 한

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

+0

이 내용을 index.php/index.html의 에 추가합니까? –

+0

@ thethelelibeseti 이것은 HTML 코드가 아닌 Android 코드입니다. 또는 귀하의 질문에 대한 오해가 있습니까? –

관련 문제