Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to handle an Android (java) string array?

How to handle an Android (java) string array?

Scheduled Pinned Locked Moved Mobile and Embedded
3 Posts 1 Posters 4.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rizzer
    wrote on last edited by
    #1

    I'm trying to use the Qt Android interface, #include <QAndroidJniObject>

    I've got the basic calls working. For instance I can create a java string with
    @QAndroidJniObject mixedCaseString = QAndroidJniObject::fromString("wHiCHcaSe");@
    and I can manipulate it, for instance
    @QAndroidJniObject lowercaseString = casedString.callObjectMethod("toLowerCase", "()Ljava/lang/String;");@
    I can confirm these are both working by converting to QStrings with mixedCaseString.toString() and lowercaseString.toString().

    I can apparently split the first string using "H" as a separator, with
    @ QAndroidJniObject splitter = QAndroidJniObject::fromString("H");
    QAndroidJniObject mixedcaseStringSplit= mixedCaseString.callObjectMethod("split", "(Ljava/lang/String;)[Ljava/lang/String;",splitter.object<jstring>());@

    The array of strings in mixedcaseStringSplit appears to be valid, and mixedcaseStringSplit.toString() returns the value (reasonable in the context, not NULL)
    @"[Ljava/lang/String;@b2e3d926"@

    But here I get stuck. How can I manipulate the elements in that array?

    Even attempts just to extract the size of the array seems to fail, e.g.
    @QAndroidJniObject mixedcaseStringSplitN = mixedcaseStringSplit.callObjectMethod("size", "()I");@
    returns NULL (more precisely, mixedcaseStringSplitN.isValid() is false)
    and
    @jint mixedcaseStringSplitNf = mixedcaseStringSplit.getField<jint>("length");@
    returns 0. From that string, the size of the split array should be 3 (["w","iC","caSe"]).

    Does anyone have a better idea on java (Android) string array handling?

    I'm using Qt 5.2.1 from Digia's online package.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rizzer
      wrote on last edited by
      #2

      Hmm, scrambling my java and C++. Java arrays have no size() method, so mixedcaseStringSplit.callObjectMethod("size", "()I") ought not to work.

      But the length of a java array is given by the length property, so mixedcaseStringSplit.getField<jint>("length") ought to work. In Qt we access java properties with getField, right?

      And once we do understand how to access java_array.length, how do we extract the elements? Java has no java_array.at( i ) function, only java_array[i]. Certainly, callObjectMethod("[]",...) does not work.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Rizzer
        wrote on last edited by
        #3

        Seems that Qt just can't do it, not directly. Have to go the long round instead, using "JNI":http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html explicitly (via the "JNI array operations":http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html#array_operations).

        With
        @#include <QAndroidJniEnvironment>
        QAndroidJniEnvironment jniEnv;@

        then

        @int splitArrayLength = jniEnv->GetArrayLength(mixedcaseStringSplit.object<jarray>());@
        gives the length of the array and
        @ QAndroidJniObject arrayElement = jniEnv->GetObjectArrayElement(mixedcaseStringSplit.object<jobjectArray>(), i );@
        gives a specific element in the array.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved