TIMAI2
May 22, 2022, 12:08pm
1
I can do this with blocks, but would like to create a little helper extension to do this.
Need to:
Import the list
get the first item
Test if first item is an "element" (e.g. string or integer)
If it is, then return the list
If it isn't, then parse the list, and return it
I can probably do the parsing code (JsonObjectEncode/JsonTextDecode), I have that already, I am struggling with items 1-3.
##
PSEUDO-CODE
@SimpleFunction(description = "fixes multi-column lists from SELECT")
public YailList ListFixer(YailList queryOutput) {
String[] queryList;
// ??
if (get.queryOutput(0).isString() || get.queryOutput(0).isInteger()) {
return queryOutput;
} else {
queryList = JsonStringify(queryOutput);
}
return YailList.makeList(queryList);
}
1 Like
@SimpleFunction
public Object ListFixer(YailList items, int pos) {
Object[] objects = items.toArray();
if (objects[0] instanceof IntNum) {
// the first element is AppInventor number type
}
}
2 Likes
Thank you - what if objects[0] is a string ? (Also why the "int pos" is this used?)
You can simply ignore that.
You have to check it the same way:
@SimpleFunction
public Object ListFixer(YailList items, int pos) {
Object[] objects = items.toArray();
Object firstElement = objects[0];
if (firstElement instanceof IntNum) {
// first element is a number
} else if (firstElement instanceof String) {
// first element is a string
} else {
// the first element is not a Number or a string
}
}
1 Like
Great, I understand
Given I can only return an Object, can I make this a YailList ?
} else {
// the first element is not a Number or a string
}
return ???YailList as Object???
}
You dont necessarily need to return it as Object, yes you can make that YailList.
@SimpleFunction public YailList ListFixer(YailList items) {...}
1 Like
I'll try it all out and report back
Thanks for your help!
1 Like
Well, you could knock my down with a feather! Worked perfectly on first build. Needed an Object after all.
Thank you so much
@SimpleFunction(description = "fixes multi-column lists from SELECT")
public Object ListFixer(YailList queryOutput) {
Object[] objects = queryOutput.toArray();
Object firstElement = objects[0];
if ((firstElement instanceof IntNum) || (firstElement instanceof String)) {
return queryOutput;
} else {
return JsonParse(JsonStringify(objects),true);
}
}
2 Likes
TIMAI2
Closed
May 29, 2022, 2:16pm
9
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.