Creating ArrayList using java jni in android

I want to create an ArrayList using using native code in android , for this I have to allow insertion of all value types into the list.

Below is my code:

std::vector<jobject> list;

extern "C"{

    JNIEXPORT void JNICALL Java_com_example_nativetestapp_NativeList_add(
            JNIEnv * env, jobject obj, jobject t) {
        __android_log_print(ANDROID_LOG_INFO,"LOG","Set %d",t); //here it is printing some integer value like -1034567334
        list.push_back(t);
    }

    JNIEXPORT jboolean JNICALL     Java_com_example_nativetestapp_NativeList_removeByIndex(
            JNIEnv * env, jobject obj, jint pos) {
        if (pos > list.size() - 1 || pos < 0)
            return false;
        return list.erase(list.begin() + pos) != NULL ? true : false;
    }

    JNIEXPORT jint JNICALL Java_com_example_nativetestapp_NativeList_size(
            JNIEnv * env, jobject obj) {
        return list.size() == NULL ? 0 : list.size();
    }

    JNIEXPORT jobject JNICALL Java_com_example_nativetestapp_NativeList_get(
            JNIEnv * env, jobject obj, int pos) {
        return list.at(pos);
    }

    JNIEXPORT jboolean JNICALL Java_com_example_nativetestapp_NativeList_contains(
            JNIEnv * env, jobject obj, jobject t) {

        for (int var = 0; var < list.size(); var++) {
            if(t==list[var])
                return true;
        }
        return false;
    }

    JNIEXPORT jboolean JNICALL Java_com_example_nativetestapp_NativeList_remove(
            JNIEnv * env, jobject obj, jobject t) {
        for (int var = 0; var < list.size(); var++) {
            if(t==list[var]){
                list.erase(list.begin()+var);
                return true;
            }
        }
        return false;
    }

};

      

I took a jobject in java jni to allow all value types, but during insertion into a list, it inserts the reference as some number, not an exact value.
And during the search, it comes back for reference only.
I tried so many ways but it doesn't work.
Please help me to solve this problem!

Thanks in advance.

+3


source to share





All Articles