0

I am a newbie in Kotlin, sorry for the silly question.

Inside a bundle from a StatusBarNotification object I have found an array of CharSequence, or at least I think that the type is that one. In the image you can see a screenshot which shows the output that I get when I evaluate the variable from within a breakpoint (in Android Studio "Debug" window->Evaluate).

enter image description here

I have tried accessing .javaClass.kotlin but I get an error so I am not sure about the type, is it an array of CharSequence?

Nevertheless, I am trying to join those strings that you see in the image into a single string. I have tried calling .toString, but it does not work, and I have tried a bunch of other methods to join elements from "iterable variables", but with poor luck.

Any help is appreciated.

Nisba
  • 3,210
  • 2
  • 27
  • 46
  • Yes it is an `Array`. Use `.joinToString()` to combine elements of Iterables or Arrays into a single string. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/join-to-string.html – Tenfour04 Mar 08 '21 at 19:57
  • @Tenfour04 I get `Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:`. Any idea? – Nisba Mar 08 '21 at 20:14
  • Does this class you're working with even publicly expose this `result` field? If you're calling `joinToString` on the class that has a `result` field *inside* it, that won't work because that class itself is not the Iterable or Array. – Tenfour04 Mar 08 '21 at 20:16
  • @Tenfour04 there is no `result` in the class, is there any way I can "evaluate" the object to get a result. I am not getting what's happening with the variable – Nisba Mar 08 '21 at 20:36
  • Is this literally a Bundle object? Is `result` one of the keys in the Bundle? – Tenfour04 Mar 08 '21 at 20:36
  • I have `StatusBarNotification` object that I call `sbn`. Then I do `val extras = sbn.notification.extras` and finally `val textLines = extra.get("android.textLines")`. – Nisba Mar 08 '21 at 20:39
  • How about `extras.getCharSequenceArray("android.textLines")?.joinToString()`? Make sure you appropriately handle nullability if the keys of this bundle aren't documented. I don't work with notifications much, so I don't know if they are guaranteed to be there. – Tenfour04 Mar 08 '21 at 20:43
  • @Tenfour04 found the culprit, thanks!!! You either need to use `getCharSequenceArray("android.textLines")` either to cast with `(get("android.textLines").as Array)` – Nisba Mar 08 '21 at 21:14

0 Answers0