Java Question Return ArrayList via B4A event

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
I am creating a library to interface to Google's Speech Recognizer. I know it's been done before, but I'd like to give it a try.

In the Recognizer Listener Java library code, I have the following:

B4X:
public void onResults(Bundle results) {
  MyLogMsg("onResults " + results);
           
  ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
           
  for (int i = 0; i < data.size(); i++) {
    MyLogMsg("  i:" + String.valueOf(i) + ", " + data.get(i));
  }

  if (ba.subExists(eventName + "_finalresult"))
    ba.raiseEvent(null, eventName + "_finalresult", data);
  }
}

Where MyLogMsg just returns a message via an Event that can be logged.

My question concerns: ArrayList<String> data. When I raiseEvent I pass the ArrayList data. How would this be received by B4A?

Would the following B4A code be correct?

B4X:
Sub CRGVR_Results(Res as List)
  For Each str As String in Res
    Log(str)
  Next
end sub

Or, what would be the best way to return the Results from Java?

Thanks,
Barry.
 

MarcoRome

Expert
Licensed User
Longtime User
Look HERE you have example by @stevel05

B4X:
....
        Case "onResults"
            Dim Results As JavaObject = Args(0)
            Dim Matches As List = Results.RunMethod("getStringArrayList",Array("results_recognition"))
            Log(Matches.Size)
            Dim Text As String = ""
            For Each Result As String In Matches
                Text = Text & Result & CRLF
            Next
            Log(Text)
            Label1.Text = Text
.....
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. You don't need to check whether the sub exists or not.
2. The default raise event method should be ba.raiseEventFromUI.
3. You need to wrap the ArrayList with anywheresoftware.b4a.objects.collections.List:
B4X:
anywheresoftware.b4a.objects.collections.List list = new anywheresoftware.b4a.objects.collections.List();
list.setObject(data);
ba.raiseEventFromUI(this, eventName + "_finalresult", list);
 

MarcoRome

Expert
Licensed User
Longtime User
Sorry @Erel, just for understand. Why do you talk about "Wrapper" if with solution @stevel05 ( way Javaobject ) already work ??
 

MarcoRome

Expert
Licensed User
Longtime User
canalrun wants to create an event and pass an ArrayList from his Java code. The correct way to do it is by creating a B4A List and settings its object property.
sorry my mistake, i missed:
I am creating a library to interface to Google's Speech Recognizer.
 

canalrun

Well-Known Member
Licensed User
Longtime User

Thanks. I will upload my Library and B4A example with source as soon as I get everything put together.

Barry.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…