Is there a simpler way to access Java arrays?
Moderator: Martin
Is there a simpler way to access Java arrays?
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.
[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?
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 2:
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: Select all
[Ljava.lang.String;@6ff7e14
Re: Is there a simpler way to access Java arrays?
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.
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.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
Re: Is there a simpler way to access Java arrays?
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)
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?
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...Code: Select all
ctx=getContext(); str=newList(); str=callJavaMethod(ctx, "android.content.Context", "fileList()"); lentgh=length(str);
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?
@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.
I will try out to see what I can do with it. It makes more sense now.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
Re: Is there a simpler way to access Java arrays?
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.
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?
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:Converting an array like this should be quite fast.
Regards,
Martin
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);
Regards,
Martin
Re: Is there a simpler way to access Java arrays?
@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.
But it throws an error during `callJavaStaticMethod()`:
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);
Does that mean that the `get()` method is not available? Or did I miss something?No method with name get found in class java.lang.reflect.Array
Re: Is there a simpler way to access Java arrays?
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
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