Sub Globals
Private ipaddress As String = "192.168.1.190" ' ip address from PC/laptop
Private url As String
Private postvars As String
Private cbx1 As B4XComboBox
Private cbx2 As B4XComboBox
Public catlst As List
Public subcatlst As List
Public itemlst As List
Public catidlst As List
Public subcatidlst As List
Public itemidlst As List
Private clv1 As CustomListView
Private B4XLoadingIndicator3 As B4XLoadingIndicator
Private RTCS As ResizingTextComponent
Private lbl1 As Label
Private lbl2 As Label
Private pnlitem As Panel
Private pnlnote As Panel
Private MediaPlayer1 As MediaPlayer
Private source As String
End Sub
The ipaddress is set to the local IP address from your PC or laptop where you run the web server on. Depending how you are connected to your local network this address may vary.Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Layout")
Activity.Title = "Thai translations"
catlst.Initialize
subcatlst.Initialize
itemlst.Initialize
catidlst.Initialize
subcatidlst.Initialize
itemidlst.Initialize
MediaPlayer1.Initialize
B4XLoadingIndicator3.show
Sleep(10)
source = "local"
Select source
Case "local"
url = "http://" & ipaddress & ":443/translations/lists_JSON.php"
postvars = "B53bLkN2ekAUHRFddzuJ=Ud4wvW4DM74VPhNsUz3y"
read_json(url, postvars)
Case "online"
' replace / with the correct information
url = "https://<your website>/<your folder>/lists_JSON.php"
postvars = "F6vjhe3D4C9gDsRSQdHF=HFPNPPQCk4wkMdESHhhg"
read_json(url, postvars)
Case Else
xui.MsgboxAsync("Unknown source","JSON source")
Exit
End Select
End Sub
The source variable is initialized to 'local' to read the JSON string from the local web server.Private Sub read_json(strurl As String, strpost As String)
Dim j As HttpJob
j.Initialize("",Me)
j.PostString(strurl,strpost)
Wait For (j) JobDone (j As HttpJob)
If j.Success Then
B4XLoadingIndicator3.Hide
Log(j.GetString)
If j.GetString.Length > 0 Then
'Log(j.GetString)
' using the parser object
'Dim parser As JSONParser
'parser.Initialize(j.GetString)
'Private tablelst As List = parser.NextArray
' using the as(JSON).ToList type conversion
Dim tablelst As List = j.GetString.as(JSON).ToList 'ignore
catlst = tablelst.Get(0)
subcatlst = tablelst.Get(1)
itemlst = tablelst.Get(2)
fill_cbx1
fill_cbx2
fill_clv1
Else
xui.MsgboxAsync("Empty JSON object!","JSON result")
Sleep(2000)
ExitApplication
End If
End If
End Sub
In this subroutine the HttpJob object uses the PostString method to send a POST request to the web server. A new thread is used and with the Wait For statement the main thread waits for the response from the web server.Private Sub fill_cbx1
catidlst.Clear
Private lst As List
lst.Initialize
For i = 1 To catlst.Size -1
Private rec As List = catlst.Get(i)
lst.Add(rec.Get(1))
catidlst.Add(rec.Get(0))
Next
cbx1.SetItems(lst)
cbx1.SelectedIndex = 0
fill_cbx2
End Sub
Private Sub fill_cbx2
subcatidlst.Clear
Private lst As List
lst.Initialize
For i = 1 To subcatlst.Size -1
Private rec As List = subcatlst.Get(i)
If rec.Get(2) = catidlst.Get(cbx1.SelectedIndex) Then
lst.Add(rec.Get(1))
subcatidlst.Add(rec.Get(0))
End If
Next
cbx2.SetItems(lst)
cbx2.SelectedIndex = 0
fill_clv1
End Sub
Private Sub fill_clv1
clv1.Clear
itemidlst.Clear
For i = 1 To itemlst.Size -1
Private rec As List = itemlst.Get(i)
If rec.Get(3) = subcatidlst.Get(cbx2.SelectedIndex) Then
Dim title As String = rec.Get(1)
If File.Exists(File.DirAssets,rec.Get(0) & ".mp3") Then
title = title & " "
End If
Dim note As String = rec.Get(2)
Dim p As B4XView = CreateItem(title, note, 400dip)
clv1.Add(p, rec.Get(0))
itemidlst.Add(rec.Get(0))
End If
Next
End Sub
In the CreateItem subroutine the layout of an item from the CustomListView is used to fill the title label (lbl1), the note panel (pnlnote) and the note label (lbl2).Sub CreateItem(Title As String,note As String, pnlHeight As Int) As B4XView
Dim p As B4XView = xui.CreatePanel("")
p.SetLayoutAnimated(0, 0, 0, clv1.AsView.Width, pnlHeight)
p.LoadLayout("Item")
p.SetLayoutAnimated(0, 0, 0, pnlitem.Width, pnlitem.Height + pnlnote.Height)
lbl1.Text = Title
lbl2.Text = note
Private lblresize As B4XView = CreateSizingItem(pnlnote.width,note)
lblresize.Color = Colors.ARGB(255,255,255,135)
pnlnote.AddView(lblresize,0,0,pnlnote.Width,lblresize.height)
pnlnote.height = lblresize.height
p.Height = pnlitem.Height + pnlnote.Height
Return p
End Sub
private Sub CreateSizingItem(wid As Int, txt As Object) As B4XView
Private p As B4XView = xui.CreatePanel("")
Activity.AddView(p,0,0,wid,300dip)
p.LoadLayout("RTCSSingle")
p.RemoveViewFromParent
RTCS.SetPadding(20dip,10dip,20dip,10dip)
RTCS.SetBackColor(Colors.ARGB(100,255,255,135))
RTCS.SetCorners(10dip)
RTCS.SetTextFont(xui.CreateFont(Typeface.LoadFromAssets("montserrat-medium.ttf"),24))
RTCS.Text = txt
p.Height = RTCS.GetHeight
Return p
End Sub
The True Type Fonts for the RTCS View can be found in the zip-file from Andrew (see B4A app layouts - RTCSSingle layout).Private Sub play_sound(id As Int)
Log(id)
If File.Exists(File.DirAssets,id & ".mp3") Then
MediaPlayer1.Load(File.DirAssets, id & ".mp3")
MediaPlayer1.Play
End If
End Sub
Private Sub cbx2_SelectedIndexChanged (Index As Int)
fill_clv1
End Sub
Private Sub cbx1_SelectedIndexChanged (Index As Int)
fill_cbx2
End Sub
Private Sub clv1_ItemClick (Index As Int, Value As Object)
play_sound(Value)
End Sub