【问题标题】:Can we use data from android to display it in webview using Javascript?我们可以使用来自android的数据使用Javascript在webview中显示它吗?
【发布时间】:2014-12-05 07:40:17
【问题描述】:

我已经使用 MySql 和 PHP 以及 JSON 解析从 Web 服务器获取数据。然后我将数据存储在arraylist中。现在我要做的是创建一个 WebView 并将这些数据插入到 html 字符串中,如下所示:

htmlData = "<html>"+
            "<head"+
            "<title>Model Question</title>"+
            "</head>"+
            "<body>"+
            "The question of "+"English is :<br />"+getfromServer.get(0).getData()[0]+ 
            "<br /><form>"+
            "Option[0]"+"<input type='radio' name='c0' /><br />"+
            "Option[1]"+"<input type='radio' name='c1' /><br />"+
            "Option[2]"+"<input type='radio' name='c2' /><br />"+
            "Option[3]"+"<input type='radio' name='c3' /><br />"+
            "</form>"+
            "</body>"+
            "</html>";

这里的 getfromServer 是 Question_answer 类型的 ArrayList,定义为:

package com.example.mcahelper;

public class Question_answer {
    private String Question,Options[] = new String[4];
    public void setData(String...datas){
        Question = datas[0];
        Options[0]=datas[1];
        Options[1]=datas[2];
        Options[2]=datas[3];
        Options[3]=datas[4];
    }
    public String[] getData(){
        String datas[]= new String[5];
        datas[0] = Question;
        datas[1]=Options[0];
        datas[2]=Options[1];
        datas[3]=Options[2];
        datas[4]=Options[3];
        return datas;
   }
}

现在我想执行循环以从数组列表中获取 Question_answer 类型的每个对象并将其放入字符串 htmlData 中,该字符串将进一步用作:

webview = (WebView) findViewById(R.id.wbvw);
    WebSettings websettings = webview.getSettings();
    websettings.setJavaScriptEnabled(true);



    webview.loadDataWithBaseURL("http://bar", htmlData,"text/html", "utf-8", "");

我们可以使用 javascript 还是他们的任何其他方式。 请帮忙。 提前致谢!

【问题讨论】:

    标签: java javascript php android mysql


    【解决方案1】:

    您为什么不简单地不遍历列表并使用StringBuilder 来构造您的html 字符串?但是,要回答您的问题,可以在 WebView 中引用来自 Javascript 的 Java 代码。请参阅此处,例如 Call java code from javascript

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 2016-10-12
      相关资源
      最近更新 更多