2

I have read many question on here, but still i don't find any right answer of my case right now

so I have a code like this

VisitReport.java

public class VisitReport{
  public static final String TABLE_NAME = "VisitReport";

  public static final String COLUMN_ID = "ID";
  public static final String COLUMN_VisitPurpose = "VisitPurpose";
  public static final String COLUMN_VisitResult = "VIsitResult";

  private int id;
  private String VisitPurpose;
  private String VisitResult;

  public static final String CREATE_TABLE =
      "CREATE TABLE " + TABLE_NAME + "("
          + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
          + COLUMN_VisitPurpose + " TEXT,"
          + COLUMN_VisitResult + " TEXT,"
          + ")";

  public VisitReport() {
  }

  public VisitReport(
    int id,
    String VisitPurpose,
    String VisitResult
  ){
    this.id = id;
    this.VisitPurpose = VisitPurpose;
    this.VisitResult = VisitResult;
  }

  public int getId() { return id; }
  public void setId(int id) { this.id = id; }
  public String getVisitPurpose() { return VisitPurpose; }
  public void setVisitPurpose(String VisitPurpose) { this.VisitPurpose = VisitPurpose;}
  public String getVisitResult() { return VisitResult; }
  public void setVisitResult(String VisitResult) { this.VisitResult = VisitResult;}
}

DatabaseHelper.java (piece of code Im used)

  public VisitReport getVisitReport(long id) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(VisitReport.TABLE_NAME,
        new String[]{
          VisitReport.COLUMN_ID,
          VisitReport.COLUMN_VisitPurpose,
          VisitReport.COLUMN_VisitResult,
        },
        VisitReport.COLUMN_ID + "=?",
        new String[]{String.valueOf(id)}, null, null, null, null);

    if (cursor != null){
      cursor.moveToFirst();
    }
    VisitReport vr = new VisitReport(
      cursor.getInt(cursor.getColumnIndex(VisitReport.COLUMN_ID)),
      cursor.getString(cursor.getColumnIndex(VisitReport.COLUMN_VisitPurpose)),
      cursor.getString(cursor.getColumnIndex(VisitReport.COLUMN_VisitResult))
    cursor.close();
    return vr;
  }

I want pass the value to react native using bridge module, but I get an error

Cannot convert argument of type class mypackage.VisitReport

here's the code I've tried :

@ReactMethod
public void getVisitReport(int id, Callback cb) {
  DatabaseHelper db = new DatabaseHelper(reactContext);
  cb.invoke(db.getVisitReport(id));
}

Find this question as my reference still I don't get what's the meaning of those because when i log the value of getVisitReport() i got mypackage.VisitReport@12d960c9 and i don't know the meaning of this

am i doing something wrong? anyone can help me how to make it work?

flix
  • 1,688
  • 3
  • 34
  • 64
  • getVisitReport() is returning an object. 'mypackage.VisitReport@12d960c9' is the string representation of that object. – valegians Jan 16 '19 at 04:08
  • @Goofynose so how can i get the value of this object? – flix Jan 16 '19 at 04:15
  • Implement toString() to get the value of the object. – Zaki Anwar Hamdani Jan 16 '19 at 04:18
  • @flix what do you mean by value? I am guessing you want to access one of the object's attributes (`id`, `visitPurpose`, ...)? You can access those values by doing something like `db.getVisitReport(id).getVisitPurpose()` – valegians Jan 16 '19 at 04:19
  • @Goofynose i want to get all of those object value, but i dont know how, i think the value of those object something like `{id:0, visitPurpose:"bar", visitResult:"baz"}` -cmiiw – flix Jan 16 '19 at 04:22
  • @ZakiAnwarHamdani have tried `toString()` but the Log show the same result – flix Jan 16 '19 at 04:28
  • @flix you need to get those values individually, that or you put them into an array that holds all of them. If you do `toString()` on getVisitReport you are doing a string conversion onto the actual `VisitReport` object itself, hence why you get the `mypackage.VisitReport@12d960c9`. Have a look at this page https://www.tutorialspoint.com/java/java_object_classes.htm – valegians Jan 16 '19 at 06:50

2 Answers2

1

At a guess something like the following may work :-

VisitReport vr = db.getVisitorReport(id);
cb.invoke("ID=" +String.valueOfvr.getid()) + ", Purporse=" + vr.getVisitPurpose"); // etc 
  • This assumes that invoke can accept a String.
MikeT
  • 51,415
  • 16
  • 49
  • 68
1

You can access each value individually by doing

db.getVisitReport(id).getVisitPurpose

Or

VisitReport vr = db.getVisitReport(id); vr.getVisitPurpose

That is because getVisitReport() returns an object. An object is an instance of a class that holds a set of variables.

When you do getVisitReport(id).toString() you are doing a returning a string representation of the object itself, not its values.

If you want to get all the object's attributes at the same time you need to create a new method inside VisitReport which returns all of the attributes in, for example, an array of objects (since all objects in Java extend Object, see How to declare an array of different data types).

Please make sure you read more about java and objects. Here is a good link: https://www.tutorialspoint.com/java/java_object_classes.htm

valegians
  • 850
  • 1
  • 7
  • 17