Page 1 of 2
Is there a simpler way to access Java arrays?
Posted: 24 Apr 2018 20:52
by jaejunks
By "Java array", I meant the one returned from below example code.
[code]result = callJavaMethod(getContext(), "android.content.Context", "fileList()");[/code]
FYI, the `fileList()` method of `android.content.Context` class returns a `String[]` value which is an array of strings.
If I pass it as the `Message` parameter of a `Message Dialog` action, it'll show as e.g.:
[code][Ljava.lang.String;@6ff7e14[/code]
And accessing the element of that Java string array can only be done using `for-each` loop. The `getElement()` function is not usable for this. And bracket notation can't be used also, since it's currently available only for number list.
Re: Is there a simpler way to access Java arrays?
Posted: 24 Apr 2018 20:59
by jaejunks
Also, there seem to be no way to know the length of that Java array, without using a `for-each` loop and manually counting the number of loops.
PS) Sorry for the disabled BBCode in the initial post. They should be like below.
Code 1:
Code: Select all
result = callJavaMethod(getContext(), "android.content.Context", "fileList()");
Code 2:
Re: Is there a simpler way to access Java arrays?
Posted: 25 Apr 2018 17:36
by Desmanto
I also stuck at the same place. Previously I want to find the timezone using java, and stuck here too. I have found a shorter method to find it anyway, but still curious about how to parse the data from the java object.
I remembered only 2 example here (you can search it using "java" keyword in my index) :
WIFI Scan: Get Level of specific BSSID?
Way to get display resolution
At there, the java object is passed to another function again. How to pass it and how to know which function to use is beyond my current knowledge.
Re: Is there a simpler way to access Java arrays?
Posted: 26 Apr 2018 14:11
by bogdyro
You can get its length and iterate through the items,yes.
ctx=getContext();
str=newList();
str=callJavaMethod(ctx, "android.content.Context", "fileList()");
lentgh=length(str);
for (s in str)
log(s)
You can also create your own list and copy the items
MyList=newList();
for(s in str)
addelement(MyList,s)
Re: Is there a simpler way to access Java arrays?
Posted: 26 Apr 2018 16:18
by jaejunks
Code: Select all
ctx=getContext();
str=newList();
str=callJavaMethod(ctx, "android.content.Context", "fileList()");
lentgh=length(str);
THAT, surprisingly works. But it just doesn't make any sense on how it works. The second line on the above code should make the `str` variable entirely a Java array value. There should be no trace or inheritance from the `List` type caused by the fist line. Passing it to the `length()` function shouldn't work, but it does. Somehow...
This, at least, solve the array length retrieval. So, thank you
bogdyro.
Now, if only there's a way to retrieve an element of the Java array without using a loop...
Re: Is there a simpler way to access Java arrays?
Posted: 26 Apr 2018 17:50
by Desmanto
@bogdyro : Thanks for the example. I don't declare the str=newList() anymore, and it still works. So when str populated by java result, it will be in the form of java object. Using the Automagic function will convert it to automagic object then (list type).
I will try out to see what I can do with it. It makes more sense now.
Re: Is there a simpler way to access Java arrays?
Posted: 26 Apr 2018 17:54
by bogdyro
Actually with or without
Str=newList() everything is the same.
Only Martin can give us more info or implement a convert function between Java types and AM variables.
Re: Is there a simpler way to access Java arrays?
Posted: 26 Apr 2018 19:51
by Martin
Hi,
Automagic has very limited support for arbitrary java types. Iterating a list using the for each loop and testing the length of an array plus function isEmpty are the only scripting features that understand regular java arrays.
You can convert the string array to a regular list of strings using a code snippet like this:
Code: Select all
ctx = getContext();
array = callJavaMethod(ctx, "android.content.Context", "fileList()");
list = newList();
for (v in array) addElement(list, v);
Converting an array like this should be quite fast.
Regards,
Martin
Re: Is there a simpler way to access Java arrays?
Posted: 26 Apr 2018 21:24
by jaejunks
@Martin: Oh, OK. I guess I'll have to use that for now. Thanks for confirming this.
I've been looking into Java classes and found that maybe I could use the `java.lang.reflect.Array.get()` static method to retrieve an element array. So I tried this.
Code: Select all
ctx = getContext();
array = callJavaMethod(ctx, "android.content.Context", "fileList()");
val = callJavaStaticMethod("java.lang.reflect.Array", "get(java.lang.Object, java.lang.Integer)", array, 0);
But it throws an error during `callJavaStaticMethod()`:
No method with name get found in class java.lang.reflect.Array
Does that mean that the `get()` method is not available? Or did I miss something?
Re: Is there a simpler way to access Java arrays?
Posted: 27 Apr 2018 08:26
by bogdyro
Nice find. In my opinion it's simpler to copy the array to a list.
But this method does work also
callJavaStaticMethod("java.lang.reflect.Array", "get(java.lang.Object, int)", name_of_array, index)
Your 'get' method signature is weird. I have 'int' instead of 'java.lang.Integer' for the second parameter