Android Question [Solved]How convert Java Code in be B4X Code

Theera

Well-Known Member
Licensed User
Longtime User
I'm creating the library about ThaiWordBreak() from Java Code which is attached as the following codes below
B4X:
Public Sub  Thaiwordbreak(InputStr As String) As String()
    #If Java
        Return ThaiwordbreakJava(InputStr)
    #End If    
End Sub
        
#If Java
/** @author Nont Banditwong
** TODO To change the template for this generated type comment go to
** Window - Preferences - Java - Code Style - Code Templates
*/


import java.text.BreakIterator;
import java.util.Locale;
 
//import com.ibm.icu.text.BreakIterator;
//import com.ibm.icu.text.DictionaryBasedBreakIterator;
 
/**
 
Private static String[] ThaiWordBreakJava(String source) {
Locale thaiLocale = new Locale(“th”);
BreakIterator boundary = BreakIterator.getWordInstance(thaiLocale);
 
//BreakIterator boundary = DictionaryBasedBreakIterator.getWordInstance(thaiLocale);
 
boundary.setText(source);

StringBuffer strout = new StringBuffer();

int start = boundary.first();
for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) {
 
strout.append(source.substring(start, end) + “-”);
}
 
//System.out.println(strout.toString());
return (strout.toString());
 
}

#End If
 

Attachments

  • ThaiWordBreak.zip
    794 bytes · Views: 15
Solution
You need to Enable the JavaObject library.

Dim twb as JavaObject
twb.initializeContext
Dim mystring as string = "your Thai string"
'Use twb = Me if you have the code in a B4XPage
mystring = twb.runMethod("ThaiWordBreakJava", Array(mystring))
Log(mystring)

Johan Schoeman

Expert
Licensed User
Longtime User
You need to Enable the JavaObject library.

Dim twb as JavaObject
twb.initializeContext
Dim mystring as string = "your Thai string"
'Use twb = Me if you have the code in a B4XPage
mystring = twb.runMethod("ThaiWordBreakJava", Array(mystring))
Log(mystring)
 
Upvote 0
Solution

Theera

Well-Known Member
Licensed User
Longtime User
If I understand to your advice, My code will be belows
Not B4XPages
B4X:
Public Sub  Thaiwordbreak(InputStr As String) As String()
   Dim twb as JavaObject
   twb.initializeContext  
  Dim mystring as string = InputStr
' Use twb = Me if you have the code in a B4XPage
mystring = twb.runMethod("ThaiWordBreakJava", Array(mystring))
'Log(mystring)
Return  mystring
End Sub

B4XPages
B4X:
Public Sub  Thaiwordbreak(InputStr As String) As String()
   Dim twb as JavaObject=Me'<--- Add  equal to Me
 ' twb.initializeContext  <---delete this line
  Dim mystring as string = InputStr
' Use twb = Me if you have the code in a B4XPage
mystring = twb.runMethod("ThaiWordBreakJava", Array(mystring))
'Log(mystring)
Return  mystring
End Sub
 
Last edited:
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Wrong for b4xPages. For b4xPages:
Dim twb as Javaobject
Dim mystring As String = "your Thai string"
twb = Me
mystring = twb.runMethod("ThaiWordBreakJava", Array(mystring))

There are 100's of samples on the forum. Just search for "b4a inline java code"
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
No, he mean this:
B4X:
Dim twb as JavaObject = Me
or i'm wrong?
When using B4XPages - yes
When normal Activity it must be

Dim twb as JavaObject
twb.initializeContext
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
I've edited at post#2, it is correct? I'm sorry I don't understand English well
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Dim twb as JavaObject=Me twb.initializeContext
You dont need twb.initializeContext this would then overwrite the “Me”
B4X:
Public Sub  Thaiwordbreak(InputStr As String) As String()
   Dim twb as JavaObject=Me
   'twb.initializeContext 'Not with B4XPages
  Dim mystring as string = InputStr
' Use twb = Me if you have the code in a B4XPage
mystring = twb.runMethod("ThaiWordBreakJava", Array(mystring))
'Log(mystring)
Return  mystring
End Sub
 
Last edited:
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
I've edited at post#2 again, it is correct
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
Thank you all of both.(Thailand has everything, but it's difficult, people who know English.)
 
Upvote 0

PaulMeuris

Well-Known Member
Licensed User
Using your java code i made a small test project.

And here's the B4A source code (B4XPages):
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Public btnclear As Button
    Public btnshow As Button
    Public etresult As EditText
    Public etsource As EditText
    Public jo As JavaObject
End Sub
Public Sub Initialize
    B4XPages.GetManager.LogEvents = True
    jo = Me
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    B4XPages.SetTitle(Me,"Thai word break")
    etsource.Text = ""
    etresult.Text = ""
End Sub
Private Sub btnshow_Click
    If etsource.Text = "" Then Return
    etresult.Text = jo.RunMethod("start",Array(etsource.text))
End Sub
Private Sub btnclear_Click
    etsource.Text = ""
    etresult.Text = ""
End Sub

#if java
import java.text.BreakIterator;
import java.util.Locale;
public String start(String source) {
    Locale thaiLocale = new Locale("th");
    BreakIterator boundary = BreakIterator.getWordInstance(thaiLocale);     
    boundary.setText(source);
    StringBuffer strout = new StringBuffer();
    int start = boundary.first();
    for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) {
        strout.append(source.substring(start, end) + "-");
        //strout.append(source.substring(start, end) + "\r\n");       
    }
    return strout.toString();
}
#End If
In B4J you just have to replace the EditText views with TextArea views.
You can display the words in a list separated by a carriage return and line feed ("\r\n").
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…