Android Question Inline Java

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to All
I have some java code that might be useful in my project. I was thinking to insert it with the "#if JAVA ... #end if" paradigm.
As a first step, I created a B4XPages project and just copied the example found in a post here ( besides my situation is more complicate). But even first trial didn't work. So I am missing something very basic, I guess..
The result is that the "FirstMethod is not found". The code is :
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private NativeMe As JavaObject
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub


Private Sub Button1_Click
    NativeMe.InitializeContext
    Dim s As String = NativeMe.RunMethod("FirstMethod", Null)
    Log(s)
End Sub

#If JAVA

public String FirstMethod() {
   return "Hello World!";
}

#End if

By the way my needs are to call a function like the following:
B4X:
class xxx
{
    public static List<Integer> YYY(double[] data1, int[] data2 , int aaa)
    { 
       List<Integer> LLL = new ArrayList<>();

       return LLL;
    }
}
 

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi. Thanks. The Java code is not as simple as I wrote here, as an example. The Java function returns a List.
You missed the static keyword.

B4X:
#If JAVA

public static String FirstMethod() {
   return "Hello World!";
}

#End if
Hi. No, it seems not to work.. I just added the keyword static Same Method not found error
B4X:
#If JAVA

public static String FirstMethod() {
   return "Hello World!";
}

#End if
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Class
import java.util.ArrayList;
or
import java.util.Map;
 
Last edited:
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
B4XPages
B4X:
Public Sub testJava
    Dim jvo As JavaObject = Me
    Dim s As String = jvo.RunMethod("FirstMethod", Null)
    Log(s)
End Sub

#If Java
public String FirstMethod () {
       return "Hello World!";
}
#End If
 
Last edited:
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Tips:
use B4X classes to call native Java.
ex.
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private JavaInline As clsJavaInline
    Private Label1 As B4XView
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
    JavaInline.Initialize
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Log(JavaInline.FirstMethod)
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    Label1.Text = JavaInline.FirstMethod
End Sub

 

Attachments

  • B4XJavaInline.zip
    10.4 KB · Views: 165
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Other
B4X:
Private Sub Button1_Click
    LogText("----- STRING -----")
    
    LogText(JavaInline.DemoStringJava)
    
    LogText("----- ARRAY STRING -----")

    For Each s As String In JavaInline.DemoArrayJava
        LogText(s)
    Next
End Sub

Public Sub LogText(sText As String)
    EditText1.Text = EditText1.Text & CRLF & sText
End Sub

Class
B4X:
Sub Class_Globals
    Private joNative As JavaObject
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    joNative = Me
End Sub

Public Sub DemoStringJava As String
    Return joNative.RunMethod("DemoStringJava", Null)
End Sub

Public Sub DemoArrayJava As String()
    Return joNative.RunMethod("DemoArrayJava", Null)
End Sub

#If Java
import java.util.ArrayList;

public String DemoStringJava () {
    return "Hello World!";
}

public String[ ] DemoArrayJava () {
    String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
    return cars;
}
#End If
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?


import java.util.ArrayList;
B4X:
    Dim Data1() As Double = Array As Double(2.5, 2.0, 3.8)
    Dim Data2() As Int = Array As Int(1, 2, 3)
    Dim Data3 As Int = 10
    Log(joNative.RunMethod("DemoArrayList", Array(Data1, Data2, Data3)))
Java:
public ArrayList<Integer> DemoArrayList(double[] data1, int[] data2, int data3)) {
    ArrayList<Integer> myNumbers = new ArrayList<Integer>();

    myNumbers.add(data2[0]);
    myNumbers.add(data2[1]);
    myNumbers.add(data2[3]);
    //.....
    return myNumbers;
}

ref:
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi. Thanks. Very good answer. Your examples work, but I wasn't able to resolve my need, related to this post. I wasn't able to extend previous examples to this case, which is the interesting one. I will try again later and confirm you. Thanks again.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
I must clarify that the problem is basically a "syntax" problem. I know what are arrays etc, But I don't figure out out how to call a function inside a java class. Thanks to your explanations, now it is enough clear for functions which are written "directly" in the java code (i mean "not inside a java class"). Of course I know what is a class and what is an array or a list. But, as I said, it is only a problem of syntax. I mean, for example, shall I write "RunMethod("ClassName.MethodName",Parms) or what else?. Or shall I ignore the java class, or shall I write an "@" before the name (just fantasy example), or, maybe, I cannot call a java method inside a class.... These are my doubts. Only a working example based on my request could clarify. Thanks again for your patience.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
From my understanding, if you write your code inside Main or B4XMainPage, it is referring to the class itself. When in runtime, it will run as b4a.example.main or b4a.example.b4xmainpage

Let say you create a standard class call MyClass, then you can put a method inside this class.

MyClass.bas:
Sub Class_Globals
    
End Sub

Public Sub Initialize
    
End Sub

Public Sub myMethod As String
    Dim jo As JavaObject = Me
    Return jo.RunMethod("myMethod", Null)
End Sub

#If Java
    public String myMethod() {
        return "Hello, I am from MyClass";
    }
#End If

Then you can call it like:
B4X:
Dim MyClass1 As MyClass
MyClass1.Initialize
Dim s As String = MyClass1.myMethod
Log(s)
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
myNumbers.add(data2[0]);
Let say you are trying to add a Double value, you will get an error.

Java:
myNumbers.add(data1[2]);

You may need to cast it as int first.
Java:
myNumbers.add((int)data1[2]);

But as I wrote, I don't see a point using a Java inline code to make it more complicated.
Why don't you just use B4X List?

At the end, you get back a java.util.ArrayList and you want to assign it to B4X object or List (ArrayList).

B4X:
Dim myNumbers As List
myNumbers.Initialize
myNumbers.Add(Data1(2).As(Int))
myNumbers.Add(Data2(2))
myNumbers.Add(Data3)
Log(myNumbers)
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Something like this?
(in main module for example)

B4X:
Dim MyInlineClass As JavaObject
MyInlineClass.InitializeNewInstance("b4?.example.main$MyInlineClass",null)  ' change to suit b4j or b4a
...
Log(MyInlineClass.RunMethod("Hello",Array("fred")))
...
End Sub

#If Java
public static class MyInlineClass{
       public MyInlineClass(){}

       public String Hello(String s){
                return "Hello there " + s;  
       }  
}
#End if
 
Upvote 1

aeric

Expert
Licensed User
Longtime User
It works but you missed a curly bracket to close the Hello function.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
B4X:
Sub Class_Globals
    Private joNative As JavaObject
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    joNative = Me
End Sub

Public Sub DemoStringJava As String
    Return joNative.RunMethod("DemoStringJava", Null)
End Sub

Public Sub DemoArrayJava As String()
    Return joNative.RunMethod("DemoArrayJava", Null)
End Sub

Public Sub SumValues As Int
    Return joNative.RunMethod("SumValues", Array(10, 20))
End Sub

Public Sub SumArray As Float
    Dim a() As Float = Array As Float(10.57, 20.53)
    Dim b() As Float = Array As Float(50.12, 100.25)
    Return joNative.RunMethod("SumArray", Array(a, b))
End Sub

Public Sub CalcArrayList As List
    Dim a() As Int = Array As Int(10, 20, 30)
    Dim b As Float = 33.35
    Dim c() As Double = Array As Double(1.7, 2.3, 3.4)
    Return joNative.RunMethod("CalcArrayList", Array(a, b, c))
End Sub

#If Java
import java.util.ArrayList;

public String DemoStringJava () {
    return "Hello World!";
}

public String[] DemoArrayJava () {
    String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
    return cars;
}

public int SumValues (int a, int b) {
    return a + b;
}

public Float SumArray (float[] a, float[] b) {
    float total = (a[0] * b[0]) + (a[1] * b[1]);
    return total;
}

public ArrayList<Object> CalcArrayList (int[] a, float b, double[] c) {
    ArrayList<Object> ResultList = new ArrayList<Object>();
    ResultList.add(a[0] + b / c[0]);
    ResultList.add(a[1] + b / c[1]);
    ResultList.add(a[2] + b / c[2]);
    return ResultList;
}

#End If
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
just to complete the class.
B4X:
Private Sub Button1_Click
    LogText(JavaInline.DemoStringJava)
    LogText("--------")
    For Each s As String In JavaInline.DemoArrayJava
        LogText(s)
    Next
    LogText("--------")
    LogText(JavaInline.SumValues)
    LogText("--------")
    LogText(JavaInline.SumArray)
    LogText("--------")
    For Each o As Double In JavaInline.CalcArrayList
        LogText(o)
    Next
    Log(EditText1.text)
End Sub

Public Sub LogText(sText As String)
    EditText1.Text = EditText1.Text & CRLF & sText
End Sub
B4X:
Sub Class_Globals
    Private joNative As JavaObject
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    joNative = Me
End Sub

Public Sub DemoStringJava As String
    Return joNative.RunMethod("DemoStringJava", Null)
End Sub

Public Sub DemoArrayJava As String()
    Return joNative.RunMethod("DemoArrayJava", Null)
End Sub

Public Sub SumValues As Int
    Return joNative.RunMethod("SumValues", Array(10, 20))
End Sub

Public Sub SumArray As Float
    Dim a() As Float = Array As Float(10.57, 20.53)
    Dim b() As Float = Array As Float(50.12, 100.25)
    Return joNative.RunMethod("SumArray", Array(a, b))
End Sub

Public Sub CalcArrayList As List
    Dim a() As Int = Array As Int(10, 20, 30)
    Dim b As Float = 33.35
    Dim c() As Double = Array As Double(1.7, 2.3, 3.4)
    Return joNative.RunMethod("CalcArrayList", Array(a, b, c))
End Sub

#If Java
import java.util.ArrayList;

public String DemoStringJava () {
    return "Hello World!";
}

public String[] DemoArrayJava () {
    String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
    return cars;
}

public int SumValues (int a, int b) {
    return a + b;
}

public Float SumArray (float[] a, float[] b) {
    float total = (a[0] * b[0]) + (a[1] * b[1]);
    return total;
}

public ArrayList<Object> CalcArrayList (int[] a, float b, double[] c) {
    ArrayList<Object> ResultList = new ArrayList<Object>();
    ResultList.add(a[0] + b / c[0]);
    ResultList.add(a[1] + b / c[1]);
    ResultList.add(a[2] + b / c[2]);
    return ResultList;
}

#End If
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…