Oct 8, 2020 at 12:54pm UTC
I'm developing an app for Android with JNI and I'm making a array list to display them in multiple spinners (dropdown) with multiple arrays
The thing is you can't assign it, so i tried other way but it could only list the first item. What did i do wrong?
Google search did not help much. It only suggested me using strcpy in char array which is not i'm looking for.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
const char *weaponsArray[] = {
OBFUSCATE("AK 47" ),
OBFUSCATE("Assault Rifle" ),
OBFUSCATE("M16" ),
OBFUSCATE("Shotgun" ),
};
const char *itemArray[] = {
OBFUSCATE("Item 1" ),
OBFUSCATE("Item 2" ),
OBFUSCATE("Item 3" ),
};
JNIEXPORT jobjectArray JNICALL
Java_com_FloatingService_getLists(JNIEnv *env, jobject activityObject, jstring featureName) {
jobjectArray ret;
const char **list= NULL;
const char *feature = env->GetStringUTFChars(featureName, 0);
if (feature == "The spinner" ){
list = weaponsArray;
}
if (feature == "The spinner 2" ){
list = itemArray;
}
int Total_Feature = (sizeof list /
sizeof list[0]);
ret = (jobjectArray)
env->NewObjectArray(Total_Feature, env->FindClass(OBFUSCATE("java/lang/String" )),
env->NewStringUTF("" ));
int i;
for (i = 0; i < Total_Feature; i++)
{
env->SetObjectArrayElement(ret, i, env->NewStringUTF(list[i]));
LOGD("list" );
}
return (ret);
}
If i initialize the array like this, it works fine and list all
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
JNIEXPORT jobjectArray JNICALL
Java_com_FloatingService_getLists(JNIEnv *env, jobject activityObject, jstring featureName) {
jobjectArray ret;
const char *list[] = {
OBFUSCATE("Item 1" ),
OBFUSCATE("Item 2" ),
OBFUSCATE("Item 3" ),
};
int Total_Feature = (sizeof list/
sizeof list[0]);
ret = (jobjectArray)
env->NewObjectArray(Total_Feature, env->FindClass(OBFUSCATE("java/lang/String" )),
env->NewStringUTF("" ));
int i;
for (i = 0; i < Total_Feature; i++)
env->SetObjectArrayElement(ret, i, env->NewStringUTF(list[i]));
return (ret);
}
Last edited on Oct 8, 2020 at 12:54pm UTC
Oct 8, 2020 at 1:03pm UTC
you should use c++, which is the string type, not char arrays, which is from C.
but if you must, you can assign them like this.
foo ("Item 1"s.data()) ;
you can't compare char arrays. (or ANY C-style arrays).
use strcmp() not ==, line 24 for example is flat out wrong.
you *CAN* use == on c++ strings.
Last edited on Oct 8, 2020 at 1:14pm UTC
Oct 8, 2020 at 3:27pm UTC
Thanks guys, I ended up doing other way
And fixed that if-statement with strcmp