React Native - 如何将可读数组从React Native映射到Android中的Native模块??

时间:2018-02-05 09:12:53

标签: java android react-native

我有一个反应原生的代码。我用它来将数组传递给本机模块,我想在本机模块上处理它,

ReadableArray的示例:

{"AuditScheduleDetailID":5525,"AuditAnswerId":33,"LocalFindingID":1,"LocalMediaID":1,"ExtFiles":"jpg"}

我想获得内部密钥的每个值 - 我该怎么做?

我试过this article,但我真的不明白。

我现在就像这样硬编码:

@ReactMethod
public void insert(ReadableArray name, String file) {
    DatabaseHandler db = new DatabaseHandler(getReactApplicationContext());
    String a = "path to file";
    for(int i=0 ; i<name.size(); i++){
    //expected result
    int audit  = 5525 //from AuditScheduleDetailID value
    int answer = 33 // from AuditAnswerId value
    int finding = 1 // from LocalFindingID value
    int media = 1 // from LocalMediaID value
    String exten = "jpg" // from ExtFiles value
    db.addContact(new Contact(audit, answer, finding, media, exten));
    }
}

有没有办法从可读阵列value获取每个key

2 个答案:

答案 0 :(得分:4)

在javascript上,您将此方法称为仅作为对象传递

import { NativeModules } from 'react-native';

NativeModules.<<ModuleName>>.insert({"AuditScheduleDetailID":5525,"AuditAnswerId":33,"LocalFindingID":1,"LocalMediaID":1,"ExtFiles":"jpg"}, <<fileName>>);

修改您的反应方法以接受ReadableMap而不是ReadableArray

@ReactMethod
public void insert(ReadableMap readableMap, String file) {
  String auditScheduleDetailID = readableMap.getString("AuditScheduleDetailID");
  String auditAnswerId = readableMap.getString("AuditAnswerId");
  String localFindingID = readableMap.getString("LocalFindingID");
  String localMediaID = readableMap.getString("LocalMediaID");
  String extFiles = readableMap.getString("ExtFiles");
  //your logic
}

答案 1 :(得分:0)

我不知道这是否是您需要的,

对于React Native >=0.62,您可以尝试以下操作:

import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.Arguments;

@ReactMethod
public void test(Callback callback) {
    WritableMap map = Arguments.createMap();

    map.putString("yourKey", "yourValue");

    callback.invoke(map);
}