将值数组从 Android Activity 传递到 WebView 中的 JavaScript
我正在使用 JS Charts 库在 Android 应用程序的 WebView 中绘制图表。我想提供 SQLite 数据库中的数据。此时此刻,我陷入了如何将数据数组从 Java 传递到 JavaScript 的困境。 JavaScript 部分期望类似的内容:
data = new Array([10, 10], [20, 10]);
我了解 addJavaScriptInterface
并且我设法将单个值从我的 Activity 传递到 WebView。只是数组给我带来了麻烦。我想到了类似的事情:
final class ChartDataLoader {
public double[][] getData() {
double[][] data = {{10, 10}, {20, 10}};
return data;
}
}
请注意,现在我只是对数据进行硬编码,但最终这将从数据库中提取。然后我将其公开给我的 JS:
webView.addJavascriptInterface(new ChartDataLoader(), "dataLoader");
最后尝试在 JavaScript 中读取它:
<html>
<head>
<script type="text/javascript" src="jscharts.js"></script>
</head>
<body>
<div id="chartcontainer">You should see a chart here.</div>
<script type="text/javascript">
myData = dataLoader.getData();
alert("DataReceived: " + myData.length);
alert("Element 0 : " + myData[0]);
var myChart = new JSChart('chartcontainer', 'line');
myChart.setDataArray(myData);
myChart.draw();
</script>
</body>
</html>
JavaScript 在这两个警报语句上失败,指出:
错误/Web 控制台(2455):未捕获 类型错误:无法读取属性 未定义的“长度” 文件:///android_asset/chart.html:15
有任何提示吗?我在网上看到一些代码,其中其他人将数组转换为字符串,然后在 JavaScript 中重新创建它,但这对我来说似乎有点过头了,我希望有更好的解决方案。另一种方法是将 XML 文件传递到图表库,但同样,我认为每次用户想要查看图表时创建一个新的 XML 会很慢。
I'm using a JS Charts library to draw graphs in a WebView of my Android Application. I want to provide the data from the SQLite database. At this moment I'm stuck on how to pass array of data from Java to JavaScript. The JavaScript part expects something like that:
data = new Array([10, 10], [20, 10]);
I know about the addJavaScriptInterface
and I managed to pass single values from my Activity to a WebView. It's only the array that gives me trouble. I thought about something like that:
final class ChartDataLoader {
public double[][] getData() {
double[][] data = {{10, 10}, {20, 10}};
return data;
}
}
Note that for now I'm just hard-coding the data, but eventually this will be pulled out from a database. So then I expose this to my JS:
webView.addJavascriptInterface(new ChartDataLoader(), "dataLoader");
And finally try to read it in JavaScript:
<html>
<head>
<script type="text/javascript" src="jscharts.js"></script>
</head>
<body>
<div id="chartcontainer">You should see a chart here.</div>
<script type="text/javascript">
myData = dataLoader.getData();
alert("DataReceived: " + myData.length);
alert("Element 0 : " + myData[0]);
var myChart = new JSChart('chartcontainer', 'line');
myChart.setDataArray(myData);
myChart.draw();
</script>
</body>
</html>
JavaScript fails on those two alert statements stating:
ERROR/Web Console(2455): Uncaught
TypeError: Cannot read property
'length' of undefined at
file:///android_asset/chart.html:15
Any hints? I saw some code online where other people convert arrays to a String and then recreate it back in JavaScript but that seems like an overkill to me and I was hoping for a better solution. An alternative is to pass an XML file to the chart library, but again, I thought it would be slow to create a new XML every time a user wants to see a graph.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要修复最近的错误,您需要将 myData 定义为 var。
您在问题末尾谈论的内容(将数组转换为字符串)是 JSON。任何上下文切换都是昂贵的(例如 Android 到 Web)。序列化/反序列化可能不会让你慢太多。
To fix the most recent error you need to define myData as a var.
What you talk about at the end of your question (converting arrays to string) is JSON. Any context switch is expensive (e.g. Android to web). The serialisation/de-serialisation probably won't slow you down too much.