ericwlange / AndroidJSCore

AndroidJSCore allows Android developers to use JavaScript natively in their apps.
468 stars 78 forks source link

array in JSObject #16

Closed krudos closed 8 years ago

krudos commented 8 years ago

i hava a JSObject with a method that return ArrayList<HashMap<String, String>> but when i call it i get a undefined type also tried to retur a object [] and get the same error, in ios i can return from a function a nsdictionary or a nsarray with no problem

does androidjscore can handle array or object [] for a JSValue?

krudos commented 8 years ago

i give you a example

test work but test2, test3 and test4 get a undefined type

@Override public Object test(String query) { return "Cheese"; }

@Override public Object test2() { return {"Cheese", "Pepperoni", "Black Olives"};

}

@Override public Object test3(String query) {

    HashMap<String, String> hmap = new HashMap<String, String>();

    hmap.put("data", "Chaitanya");

    return hmap;

}

@Override public Object test4(String query) {

    HashMap<String, String> hmap = new HashMap<String, String>();

    hmap.put("data", "Chaitanya");

 HashMap<String, String> hmap2 = new HashMap<String, String>();

    hmap2.put("data2", "Chaitanya2");

    return {hmap, hmap2};

}
ericwlange commented 8 years ago

AndroidJSCore knows how to convert a number of simple types, including strings, integers, doubles, booleans. It doesn't know what to do with complex Java types. In the end, everything must be converted to a JSValue. It looks like you are trying to create dictionaries to pass to JS. To do that, you need to create a JSObject and set its properties. For example, in test4:

public JSValue[] test4()
{
    JSObject foo = new JSObject(context);
    foo.property("data", "Chaitanya");
    JSObject foo2 = new JSObject(context);
    foo2.property("data2", "Chaitanya2");
    return {foo, foo2};
}

This should work, though full disclosure I didn't test this so it may have errors in it.

ericwlange commented 8 years ago

Please re-open if this doesn't solve your issue.