本文整理汇总了C++中QAndroidJniEnvironment::NewObject方法的典型用法代码示例。如果您正苦于以下问题:C++ QAndroidJniEnvironment::NewObject方法的具体用法?C++ QAndroidJniEnvironment::NewObject怎么用?C++ QAndroidJniEnvironment::NewObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QAndroidJniEnvironment
的用法示例。
在下文中一共展示了QAndroidJniEnvironment::NewObject方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: createHashMap
static jobject createHashMap(const QVariantMap &data) {
QAndroidJniEnvironment env;
jclass mapClass = env->FindClass("java/util/HashMap");
if (mapClass == NULL) {
qWarning() << "Failed to find class" << "java/util/HashMap";
return NULL;
}
jclass integerClass = env->FindClass("java/lang/Integer");
jmethodID integerConstructor = env->GetMethodID(integerClass, "<init>", "(I)V");
jclass booleanClass = env->FindClass("java/lang/Boolean");
jmethodID booleanConstructor = env->GetMethodID(booleanClass,"<init>","(Z)V");
jsize map_len = data.size();
jmethodID init = env->GetMethodID(mapClass, "<init>", "(I)V");
jobject hashMap = env->NewObject( mapClass, init, map_len);
jmethodID put = env->GetMethodID( mapClass, "put",
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
QMapIterator<QString, QVariant> iter(data);
while (iter.hasNext()) {
iter.next();
// qDebug() << iter.key() << iter.value();
QString key = iter.key();
jstring jkey = env->NewStringUTF(key.toLocal8Bit().data());
QVariant v = iter.value();
if (v.type() == QVariant::String) {
QString str = v.toString();
jstring vString = env->NewStringUTF(str.toLocal8Bit().data());
env->CallObjectMethod(hashMap,put,jkey,vString);
} else if (v.type() == QVariant::Int) {
jobject integer = env->NewObject(integerClass,integerConstructor,v.toInt());
env->CallObjectMethod(hashMap,put,jkey,integer);
} else if (v.type() == QVariant::Bool) {
jobject boolean = env->NewObject(booleanClass,booleanConstructor,v.toBool());
env->CallObjectMethod(hashMap,put,jkey,boolean);
} else {
qWarning() << "QASystemDispatcher: Non-supported data type - " << v.type();
}
}
if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
env->ExceptionClear();
}
return hashMap;
}