flix
flix

Reputation: 1974

How to get row of sqlite after insert in android and pass it to react native?

I'm new in android, and i'm confused about getting value after inserting some data into sqlite, learning from this tutorial, there's some way to get row by id,

after following the code, I can't Log those value to pass into react native,

here's the code I've tried:

//java bridge
@ReactMethod
public void insertNote(String Note, Callback successCb){
  long id;
  DatabaseHelper db = new DatabaseHelper(reactContext);
  id = db.insertNote(Note);
  Note note = db.getNote(id);
  if(note !=null){
    successCb.invoke(note);
  } else {
    //pass error callback
  }
}

//Javascript code
MyModule.insertNote("Try Insert Note to sqlite", (success)=>{console.log(success)})

I want to add callback to show the data already inserted, but above code showing error

E/unknown:ReactNative: Exception in native call
    java.lang.RuntimeException: Cannot convert argument of type class com.digifc.sqlite.Note
        at com.facebook.react.bridge.Arguments.fromJavaArgs(Arguments.java:191)
        at com.facebook.react.bridge.CallbackImpl.invoke(CallbackImpl.java:32)
        at com.digifc.sqlite.RNDigiFcSqliteModule.insertNote(RNDigiFcSqliteModule.java:79)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)
        at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:160)
        at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29)
        at android.os.Looper.loop(Looper.java:135)
        at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:192)
        at java.lang.Thread.run(Thread.java:818)

someone can help me to fix the issue?

EDIT: Reference : What is cause for this issue "Cannot convert argument of type class org.json.JSONArray" in react-native android?

try to modify java code like this:

@ReactMethod
public void insertNote(String Note, Callback successCb){
  long id;
  DatabaseHelper db = new DatabaseHelper(reactContext);
  id = db.insertNote(Note);
  Note note = db.getNote(id);
  WritableMap resultData = new WritableNativeMap();
  if(note !=null){
    resultData.putString("Value", String.valueOf(note));
    successCb.invoke(resultData);
  } else {
    //pass error callback
  }
}

I got the result I/ReactNativeJS: { Value: 'com.digifc.sqlite.Note@12d960c9' } but the expected result should be I/ReactNativeJS: { Value: 'Try Insert Note to sqlite' }

Upvotes: 0

Views: 558

Answers (1)

10101010
10101010

Reputation: 1821

Instead of

resultData.putString("Value", String.valueOf(note));

use

resultData.putString("Value", note.getNote());

Here note is referring to the Note class.
The class has a getNote method which returns a string which represents the text you are looking for.

Upvotes: 0

Related Questions