Reputation: 55
I'm working on a 3 track step sequencer, and the spinners for sample selection are causing me some trouble. The following code is executed when a saved pattern is loaded to set the correct samples.
for ((index, j) in spinners.withIndex()){
val item = loadingPattern[(48+index)]
Log.i("item", item.toString())
Log.i("int", item.toInt().toString)
j.setSelection(item.toInt())
}
The array "spinners" contains 3 spinners with 5 entries each. "loadingPattern" is a string that contains the pattern information. The last three characters are integers corresponding to the spinner position. When the spinner selections are as the following:
Spinner 1: Selected item index 0 Spinner 2: Selected item index 3 Spinner 3: Selected item index 4
The log "item" prints exactly these values. The log "int" however prints 48+index, so in this case 48, 50, 52. Since the toInt() function is also called on the value parsed to the .setSelection() function, this exception is triggered:
java.lang.ArrayIndexOutOfBoundsException: length=2; index=48
If anyone has an idea why this is happening, I would be very happy to know. Thank you all so much! I love this communtiy!
edit: If the pattern is programmed like this: typical pattern
The "patternString" is the following: 101010101010101010000000100000001000100010001000420
active steps are represented by 1, the others by 0. The last 3 digits are the spinner positions. Maybe this will help :)
Edit 2: I just found this question: I have String with numbers when getting the char from string it shows '0'48 in kotlin. How to get the char alone in kotlin And there they say that the ASCII value of 0 is 48. This means that toInt() probably returns the ASCII value of 0. But why is that and how can I get just the normal integer?
Upvotes: 1
Views: 1939