2

I have a code in react native. I use it to pass array to native module, and I want to handle it on native module,

Example of ReadableArray:

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

I want to get each value of key inside - how can I do that?

I've tried this article but I dont really understand it.

I just hardcoded it for now like this :

@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));
    }
}

Is there a way to get each value from readablearray key?

2 Answers 2

4

On javascript you call this method just passing as object

import { NativeModules } from 'react-native';

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

Modify your react method to accept ReadableMap instead of 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
}
Sign up to request clarification or add additional context in comments.

Comments

3

I don't know if this is what you need but,

For React Native >=0.62, you can try this:

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.