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
java c ++ android


source to share


No one has answered this question yet

Check out similar questions:

6170
Is Java "pass-by-reference" or "pass-by-value"?
3799
How do I read / convert an InputStream to a string in Java?
3606
Close / hide Android soft keyboard
3393
Create ArrayList from array
3295
Why is the Android emulator so slow? How can we speed up the development of an Android emulator?
3288
Correct use cases for Android UserManager.isUserAGoat ()?
3044
Making a memory leak with Java
2936
When to use LinkedList over ArrayList in Java?
2583
Initializing an array in one line
7
JNI passes parameters to C ++ method



All Articles
Loading...
X
Show
Funny
Dev
Pics