Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Convert Android Java String[] array to C++ vector<string>



  • I have a snippet like this:

    //Java
    static String[] stringArray(String s1, String s2) { ... }
    
    //C++
    QAndroidJniObject stringArray = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/TestClass",
                                                                              "stringArray"
                                                                              "(Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;"
                                                                               string1.object<jstring>(),
                                                                               string2.object<jstring>());
    

    Having stringArray how to convert it to vector<string>?



  • @Kyeiv said in Convert Android Java String[] array to C++ vector<string>:

    Having stringArray how to convert it to vector<string>?

    You can do it like this:

    QAndroidJniEnvironment jniEnv;
    QStringList stringList;
    
    if(stringArray.isValid())
    {
        const jobjectArray stringListObjArray = stringArray.object<jobjectArray>();
        const int arraySize= jniEnv->GetArrayLength(stringListObjArray );
        
        for(int idx = 0; idx < arraySize; ++idx)
        {
            const QAndroidJniObject stringObj = QAndroidJniObject::fromLocalRef(jniEnv->GetObjectArrayElement(stringListObjArray , idx));
            stringList << stringObj .toString();
        }
    }
    


  • @Kyeiv said in Convert Android Java String[] array to C++ vector<string>:

    Having stringArray how to convert it to vector<string>?

    You can do it like this:

    QAndroidJniEnvironment jniEnv;
    QStringList stringList;
    
    if(stringArray.isValid())
    {
        const jobjectArray stringListObjArray = stringArray.object<jobjectArray>();
        const int arraySize= jniEnv->GetArrayLength(stringListObjArray );
        
        for(int idx = 0; idx < arraySize; ++idx)
        {
            const QAndroidJniObject stringObj = QAndroidJniObject::fromLocalRef(jniEnv->GetObjectArrayElement(stringListObjArray , idx));
            stringList << stringObj .toString();
        }
    }
    

Log in to reply