I have the following Java interfaces,
public interface JavascriptInterface
{
public MyIterator getItems();
}
public interface MyIterator
{
public boolean hasNext();
public String next();
}
which I expose as an API to a JavaScript web application:
Webview webview;
JavascriptInterface webapi;
//init webview and webapi [..]
webview.addJavascriptInterface(webapi, "webapi");
I wonder if there is a possibility to iterate over the items with a JavaScript for-loop like this:
for (var item in webapi.getItems())
{
//do something with item [..]
}
What type/methods do I have to use/implement instead of MyIterator to make this possible, or can I convert it somehow (with a JavaScript helper)?
I currently return a String object with JSON and parse it with a JavaScript library. Is there an easier/faster solution?
Edit: The JSON solution also has the issue, that I can only provide a copy of static data and can not invoke methods.
If Android would fully support JavaScript version 1.7, I could to something like this, to map between Java and JavaScript Iterable (unfortunately they does not support it yet, but maybe this will work in the future):
or
Note: This time I assume
webapi.getItems()returns a JavaIterable<T>.I dont know if
__iterator__()is a valid method name in Java, but you could also think about returning a custom Iterable/Iterator interface in Java, implementing__iterator__(), andnext(). The only question is, how to throw theStopIterationexception.