传递数值数组从Android的活动给JavaScript在web视图 [英] Passing an array of values from Android Activity to JavaScript in a WebView

查看:124
本文介绍了传递数值数组从Android的活动给JavaScript在web视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是JS图表库绘制图表在我的Andr​​oid应用程序的web视图。我想从SQLite数据库提供数据。在这一刻我卡在如何从Java传递数据的数组的JavaScript。 JavaScript的一部分,希望这样的事情:

 数据=新的Array([10,10],[20,10]);
 

我了解的 addJavaScriptInterface 的,我设法从我的活动传递单个值到的WebView。这只是,给我捣乱的阵列。我想过这样的事情:

  final类ChartDataLoader {
 

大众双[] []的getData(){    双[] []数据= {{10,10},{20,10}};    返回的数据;   }  }

请注意,就目前而言,我只是硬编码的数据,但最终这将是从数据库中拉出。于是我揭露这对我的JS:

  webView.addJavascriptInterface(新ChartDataLoader()的DataLoader);
 

最后试着读它的JavaScript:

 < HTML>
< HEAD>
<脚本类型=文/ JavaScript的SRC =jscharts.js>< / SCRIPT>
< /头>

<身体GT;
< D​​IV ID =chartcontainer>您应该看到一个图表此处< / DIV>

<脚本类型=文/ JavaScript的>

 myData的= dataLoader.getData();
 警报(DataReceived:+ myData.length);
 警报(元素0:+ myData的[0]);

 VAR myChart =新JSChart('chartcontainer','行');
 myChart.setDataArray(myData的);
 myChart.draw();

< / SCRIPT>
< /身体GT;
< / HTML>
 

JavaScript的失败在这两个警告声明称:

  

ERROR / Web控制台(2455):未捕获   类型错误:无法读取属性   未定义的长度的   文件:///android_asset/chart.html:15

任何提示?我看到一些code网上,其他人数组转换为字符串,然后重新回来在JavaScript,但似乎像矫枉过正我,我希望一个更好的解决方案。另一种方法是通过一个XML文件,图表库,但同样,我认为这将是缓慢的每一个用户想要查看图表的时间来创建一个新的XML。

解决方案
  

删除行:警报(DataReceived:+ myData.length);

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.

解决方案

Remove the line: alert("DataReceived: " + myData.length);

这篇关于传递数值数组从Android的活动给JavaScript在web视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆