4

I know there a couple of threads similar to this one but they dont actually answer the above question. Firts question: is it true that only primitives can be passed? (String, boolean,...) Second question: if it is so. I have an array of String in my activiy and i need it to fill a html table in my WebView and apparently i need to use Javascript interface to do so. So the question is: How can i do that? Do I need to create a string in my activity, pass it to JS and once there recreate the array?

3 Answers 3

2

You could use JSON as format for your data. A simple way would be to use a lib like GSON http://code.google.com/p/google-gson/ which makes it easy to convert your ArrayList with own object-types to Strings.

Send that to your WebView via the Javascript-interface and use JSON.parse(Stringname) in JS to recreate your Array.

Best wishes, Tim

Sign up to request clarification or add additional context in comments.

2 Comments

Danke Tim. I will give it a try. Someone also suggested me to try out JQuery. What do you think?
I really like jQuery on Android in WebViews (currently working on a project with that). It's fine to manipulate the DOM tree. Any questions?
1

Your option is to expose the method using strings and then you can use the JSONObject or JSONArray to parse the string and use it accordingly.

Here is what I did.

@JavascriptInterface
public void passJSON(String array, String jsonObj) throws JSONException
{
    JSONArray myArray = new JSONArray(array);
    JSONObject myObj = new JSONObject(jsonObj);     
    ...

}

where array is '["string1","string2"]' and jsonObj is '{attr:1, attr2:"myName"}'

Comments

1

"Do I need to create a string in my activity, pass it to JS and once there recreate the array?" That's the way i resolved it in my case ; i appended a special character to each item in the list while building up the string and later split it up in JS.

var strings = get.list(); // long string from activity.
var item1 = strings.split("=")[0];
var item2 = strings.split("=")[1];

.... Or you could go for a library

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.