Passing a list of string as an argument to javascript function from native java code

user997248

I have a javascript function:

drawPath: function drawPathFn(nodeList){
        console.log(""+nodeList[1]);
},

and the native java code that calls this function is:

List<String> nodes = getShortestPath(s, d);
architectView.callJavascript("World.drawPath('"+nodes+"')");

nodes list is filled with several location names but when I try to pass this list to the javascript function, the console output is just: "[" for console.log(""+nodeList[0]); and "S" for console.log(""+nodeList[1]);

What I want is that when I call nodeList[0], I want it to print out e.g. "Building A". How could I achieve that?

Daniel Sunami

You'll need to pass a JSObject or a String as a literal array in javascript, i.e. "['str0','str1']". Here's how to use a JSObject:

//first we need an Iterator to iterate through the list
java.util.Iterator it = nodes.getIterator();
//we'll need the 'window' object to eval a js array, you may change this
//I dont know if you are using an applet or a javaFX app. 
netscape.javascript.JSObject jsArray = netscape.javascript.JSObject.getWindow(YourAppletInstance).eval("new Array()");
//now populate the array 
int index = 0;
while(it.hasNext()){
  jsArray.setSlot(index, (String)it.next());
  index++;
}
//finaly call your function
netscape.javascript.JSObject.getWindow(YourAppletInstance).call("World.drawPath",new Object[]{jsArray});

Here's how to do it with a literal String:

java.util.Iterator it = nodes.getIterator();
int index = 0;
String literalJsArr = "[";
//populate the string with 'elem' and put a comma (,) after every element except the last 
while(it.hasNext()){
  literalJsArr += "'"+(String)it.next()+"'";
  if(it.hasNext() ) literalJsArr += ",";
  index++;
}
literalJsArr += "]"; 
architectView.callJavascript("World.drawPath("+literalJsArr+")");

reference:

http://www.oracle.com/webfolder/technetwork/java/plugin2/liveconnect/jsobject-javadoc/netscape/javascript/JSObject.html https://docs.oracle.com/javase/tutorial/deployment/applet/invokingJavaScriptFromApplet.html

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

passing array as javascript function argument from php

From Dev

Passing function pointer from native to java

From Dev

Passing the elements of an array as argument list to a function (not as a combined string)

From Dev

Passing a List as Function Argument R

From Dev

Passing null string to function as an argument

From Dev

Passing a string as a keyed argument to a function

From Dev

Passing spaced string as argument to function

From Dev

Passing Integer List from Java to Oracle Function

From Dev

JavaFX Java passing argument string from controller to controller

From Dev

I am giving passing the argument into function still it gives error code missing argument while adding linked list

From Javascript

Call JavaScript function from native code in WKWebView

From Dev

Call JavaScript function from native code

From Dev

Calling javascript function from android native code

From Dev

Passing a linked list as an argument to a function in C

From

Go - Passing an array to a function receiving argument list

From Dev

Passing a function of a class as argument to call it on a list of classes

From Java

Passing Java ArryList<String> to JNI C function and print the list in C

From Dev

Passing an anonymous function as an argument but it treats it as a string

From Dev

Passing string argument to jQuery and it thinks its function

From

Passing a string with spaces as a function argument in bash

From Dev

Passing a file as one string argument to a bash function

From Dev

Passing the string argument of a function as a column name

From Dev

Passing string from native module to react native

From Dev

Passing div style as a function argument Javascript/ React

From Dev

Passing argument to the function inside the object in Javascript

From Dev

Passing javascript object as an argument to a function - typescript equivalent

From Dev

Passing an argument to a Javascript function through PHP

From Dev

Passing an Object as an Argument in Javascript - But not simply an Argument but an argument built from variables

From Dev

Javascript, passing my string as an argument turns it to a 0

Related Related

  1. 1

    passing array as javascript function argument from php

  2. 2

    Passing function pointer from native to java

  3. 3

    Passing the elements of an array as argument list to a function (not as a combined string)

  4. 4

    Passing a List as Function Argument R

  5. 5

    Passing null string to function as an argument

  6. 6

    Passing a string as a keyed argument to a function

  7. 7

    Passing spaced string as argument to function

  8. 8

    Passing Integer List from Java to Oracle Function

  9. 9

    JavaFX Java passing argument string from controller to controller

  10. 10

    I am giving passing the argument into function still it gives error code missing argument while adding linked list

  11. 11

    Call JavaScript function from native code in WKWebView

  12. 12

    Call JavaScript function from native code

  13. 13

    Calling javascript function from android native code

  14. 14

    Passing a linked list as an argument to a function in C

  15. 15

    Go - Passing an array to a function receiving argument list

  16. 16

    Passing a function of a class as argument to call it on a list of classes

  17. 17

    Passing Java ArryList<String> to JNI C function and print the list in C

  18. 18

    Passing an anonymous function as an argument but it treats it as a string

  19. 19

    Passing string argument to jQuery and it thinks its function

  20. 20

    Passing a string with spaces as a function argument in bash

  21. 21

    Passing a file as one string argument to a bash function

  22. 22

    Passing the string argument of a function as a column name

  23. 23

    Passing string from native module to react native

  24. 24

    Passing div style as a function argument Javascript/ React

  25. 25

    Passing argument to the function inside the object in Javascript

  26. 26

    Passing javascript object as an argument to a function - typescript equivalent

  27. 27

    Passing an argument to a Javascript function through PHP

  28. 28

    Passing an Object as an Argument in Javascript - But not simply an Argument but an argument built from variables

  29. 29

    Javascript, passing my string as an argument turns it to a 0

HotTag

Archive