2012-01-24 13 views
99

Hola Estoy analizando xml y luego lo estoy cargando a la vista web, después del análisis estoy creando cuatro cadenas para poder agregar toda la cadena a una vista. Puedo obtener dos vistas en la vista web, pero no las dos primeras.Cómo pasar cadena html a webview en android

Pls sugiérame con mi código, donde me estoy equivocando y cuál es la forma correcta de obtener las cadenas html formateadas en la vista web. Pls eche un vistazo a mi código y ayúdenme a resolver este problema.

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

yo soy capaz de conseguir chapterIntro y chaptercontent en la vista web, pero no los pls dos primeras cuerdas ayudarme amigos.

Respuesta

115

Para cargar sus datos en WebView. Llamar loadData() método de WebView

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

Puede comprobar este ejemplo

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

+0

Traté con eso, todavía no está ocurriendo también trató con loadbaserul – cavallo

+0

se supone que trabajar con una cadena que contiene javascript? No funciona para mí – Edu

+22

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

137

he hecho con éxito por debajo de la línea

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

¿agrega esto al en el index.php/index.html? –

+1

ya si lo desea, de lo contrario estará bien –

+0

Actualiza la IU después de un pequeño retraso. ¿Cómo arreglar eso? – NarendraJi

18

Pasar NULL sería mejor. Los códigos completos es como:

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

¿lo agrega al en el index.php/index.html? –

+0

@mthethelelibeseti esto es códigos Android, no HTML. ¿O no entendí bien tu pregunta? –

Cuestiones relacionadas