7. B4XDialogs
When the user clicks or taps on the bottom left button with the + sign the show_adddialog subroutine is called.
Private Sub btnadd_Click
If tablename <> "" Then
show_adddialog
End If
End Sub
For each field a panel with a fieldname Label and an EditText view is set up.
Then these panels are added to the ScrollView (svadd).
private Sub show_adddialog
Dim pnl1 As Panel = setup_adddialog("adddialog_layout")
Dim rsub1 As ResumableSub = adddialog.ShowCustom(pnl1, "OK", "", "CANCEL")
Wait For (rsub1) Complete (Result As Int)
If Result = xui.dialogResponse_Positive Then
' views van pnl1 met tags "edt_" & i overlopen en in valuelist stoppen
Dim valuelst As List
valuelst.Initialize
For Each vw As View In pnl1.GetAllViewsRecursive
Dim tg As String = vw.Tag
If tg.StartsWith("edt_") Then
valuelst.Add(vw.As(EditText).Text)
End If
Next
Log(valuelst)
If valuelst.Get(0) <> "" Then
db.add_record(tablename, valuelst)
db.export_database(databasename,exportfolder,importfolder,databasename)
fill_B4XTable
End If
End If
End Sub
private Sub setup_adddialog(strlayout As String) As Panel
Dim pnl As B4XView = xui.CreatePanel("")
pnl.SetLayoutAnimated(0dip, 0dip, 0dip, 340dip, 200dip)
pnl.LoadLayout(strlayout)
pnl.RemoveAllViews
Dim lbl As Label = set_label("Add record to " & tablename,Colors.ARGB(100,255,232,132),"",18)
pnl.AddView(lbl,0dip,0dip,340dip,30dip)
svadd.RemoveView
Dim lst As List = db.get_fieldnames(tablename)
For i = 1 To lst.Size - 1
Dim edt As EditText
edt.Initialize("")
edt.Tag = "edt_" & i
Dim p1 As Panel = set_field_panel(lst.Get(i),edt,60dip)
svadd.panel.AddView(p1,0dip,(i-1)*80dip,340dip,80dip)
Next
svadd.Panel.Height = (lst.Size-1)*80dip + 300dip '1000dip
svadd.Panel.Color = Colors.ARGB(100,249,249,221)
svadd.Color = Colors.ARGB(100,249,249,221)
pnl.AddView(svadd,0dip,30dip,340dip,400dip)
Return pnl
End Sub
Private Sub set_field_panel(fldname As String, e1 As EditText,fldheight As Int) As Panel
Dim fldpnl As Panel
fldpnl.Initialize("")
Dim lblhint As Label = set_label(fldname,Colors.ARGB(100,255,255,97),"",12)
fldpnl.AddView(lblhint,0dip,0dip,170dip,20dip)
e1.Color = Colors.White
fldpnl.AddView(e1,0dip,20dip,340dip,fldheight)
Dim l1 As Label = set_label("",Colors.Black,"",1)
fldpnl.AddView(l1,0dip,20dip+fldheight-1,340dip,1dip)
fldpnl.Color = Colors.ARGB(100,249,249,203)
Return fldpnl
End Sub
private Sub set_label(lbltext As String, intbackground As Int, lbltag As String, txtsize As Int) As Label
Private lbl As Label
lbl.Initialize(lbltag)
lbl.Background = set_background(intbackground)
lbl.Typeface = Typeface.DEFAULT_BOLD
lbl.TextSize = txtsize
lbl.Text = lbltext
lbl.Gravity = Bit.Or(Gravity.LEFT,Gravity.TOP)
lbl.Padding = Array As Int (5dip, 0dip, 3dip, 0dip)
Return lbl
End Sub
Private Sub set_background(intcolor As Int) As StateListDrawable
Private cd,cd2 As ColorDrawable
Private sd As StateListDrawable
cd.Initialize2(intcolor,5,1,Colors.Black)
cd2.Initialize2(Colors.Red,5,1,Colors.Black)
sd.Initialize
sd.AddState(sd.State_Pressed,cd2)
sd.AddState(sd.State_Enabled,cd)
Return sd
End Sub
When the user clicks or taps on the export button (the button in the bottom right corner of the screen) then the show_exportdialog subroutine is called.
Private Sub btnexport_Click
show_exportdialog
End Sub
private Sub show_exportdialog
Dim pnl As B4XView = xui.CreatePanel("")
pnl.SetLayoutAnimated(0, 0, 0, 340dip, 150dip)
pnl.LoadLayout("exportdialog_layout")
Dim rs As ResumableSub = exportdialog.ShowCustom(pnl, "OK", "", "CANCEL")
ftfexportdb.Text = databasename
Wait For (rs) Complete (Result As Int)
If Result = xui.DialogResponse_Positive Then
Log(ftfexportdb.Text)
If ftfexportdb.Text <> "" Then
db.export_database(databasename,exportfolder,importfolder,ftfexportdb.Text)
End If
End If
End Sub
When the user clicks or taps on a cell in the B4XTable then one of 3 B4XDialogs can be called.
If the cell is part of the first column (rowid) then the show_remove_dialog is called.
If the cell is part of a column that has a BLOB or GRAPHIC type indication then the show_photodialog is called.
In all other cases the show_change_dialog is called.
Private Sub B4XTbl1_CellClicked (ColumnId As String, RowId As Long)
Dim RowData As Map = B4XTbl1.GetRow(RowId)
Dim cell As String = RowData.Get(ColumnId)
Dim recid As Int = RowData.Get("rowid")
'Log(cell)
Dim fname As String = ColumnId
fname = fname.SubString2(1,fname.Length-1) ' remove []
Log(fname)
If ColumnId <> "rowid" Then
'Log(recid)
If cell <> "BLOB" And cell <> "GRAPHIC" Then
show_change_dialog(recid,fname)
Else
show_photodialog(recid,fname)
End If
Else
'Log(recid)
show_remove_dialog(recid)
End If
End Sub
Private Sub show_change_dialog(rowid As Int, colid As String)
'Dim fldlst As List = db.get_fieldnames(tablename)
Dim pnl1 As Panel = setup_dialog("changedialog_layout"," Edit " & tablename & " rowid=" & rowid, Colors.ARGB(100,255,232,132))
Dim rsub1 As ResumableSub = changedialog.ShowCustom(pnl1, "SAVE", "", "CANCEL")
Private rs1 As ResultSet = db.find_record(tablename,rowid)
If rs1.NextRow Then
ftf1.Text = rs1.GetString(colid)
End If
ftf1.HintText = colid
ftf1.Update
Wait For (rsub1) Complete (Result As Int)
If Result = xui.dialogResponse_Positive Then
If ftf1.Text <> "" Then
db.change_field_content(tablename, Array As String(ftf1.Text),rowid,colid)
db.export_database(databasename,exportfolder,importfolder,databasename)
fill_B4XTable
End If
End If
End Sub
Private Sub show_remove_dialog(rowid As Int)
'Dim fldlst As List = db.get_fieldnames(tablename)
Dim pnl1 As Panel = setup_dialog("changedialog_layout"," Remove " & tablename & " rowid=" & rowid, Colors.ARGB(100,255,153,153))
Dim rsub1 As ResumableSub = changedialog.ShowCustom(pnl1, "YES", "NO", "CANCEL")
Private rs1 As ResultSet = db.find_record(tablename,rowid)
If rs1.NextRow Then
ftf1.Text = rs1.GetString2(1)
End If
ftf1.HintText = ""
ftf1.Update
Wait For (rsub1) Complete (Result As Int)
If Result = xui.dialogResponse_Positive Then
If ftf1.Text <> "" Then
db.remove_record(tablename,rowid)
db.export_database(databasename,exportfolder,importfolder,databasename)
fill_B4XTable
End If
End If
End Sub
private Sub setup_dialog(strlayout As String,lbltext As String,lblcolor As Int) As Panel
Dim pnl As B4XView = xui.CreatePanel("")
pnl.SetLayoutAnimated(0, 0, 0, 340dip, 200dip)
pnl.LoadLayout(strlayout)
Dim lbl As Label
lbl.Initialize("")
lbl.Text = lbltext
lbl.TextSize = 18
lbl.TextColor = Colors.Black
lbl.Color = lblcolor
pnl.AddView(lbl,0,0dip,340dip,50dip)
Return pnl
End Sub
The show_photodialog tries to display the BLOB or GRAPHIC that is in the database.
The sample code in the comments changes the content of a BLOB field in the database (EDIT: Android version)
That image can be displayed where others can not.
You can do some tests with the demo database that comes with the SQLite Expert Personal windows program.
EDIT: you can test the content of the blob by using a Hex dump of the buffer. Add the ByteConverter library.
If the Hex string contains certain hex-values than you can determine which image format it is and at which offset to read the buffer. Initialize the inputstream accordingly.
You can find the Hex strings to use for testing in this list:
Wikipedia: list of file signatures.
Now only one problem left to solve: the GRAPHIC field type doesn't show in the photodialog.
private Sub show_photodialog(rowid As Int,colid As String)
Dim pnl As B4XView = xui.CreatePanel("")
pnl.SetLayoutAnimated(0dip, 0dip, 0dip, 340dip, 500dip)
pnl.LoadLayout("photodialog_layout")
pnl.RemoveAllViews
' get BLOB from database
Dim rs As ResultSet = db.find_record(tablename,rowid)
rs.NextRow
Try
Log(rs.GetString("rowid"))
Log(rowid)
Log(colid)
' saving an example image to the database
' ivphotodialog.Bitmap = LoadBitmapResize(exportfolder,"angelfish.jpg",300dip,300dip,True)
' Dim bmp As Bitmap = ivphotodialog.Bitmap
' Dim OutputStream1 As OutputStream
' OutputStream1.InitializeToBytesArray(1000)
' bmp.WriteToStream(OutputStream1, 90, "JPEG")
' Dim Buffer() As Byte = OutputStream1.ToBytesArray
' db.change_field_content(tablename,Array As Object(Buffer),rowid,colid)
'
Dim Buffer() As Byte = rs.GetBlob(colid)
Log(Buffer.Length)
Dim bc As ByteConverter
Dim hex As String = bc.HexFromBytes(Buffer)
Log(hex)
Dim InputStream1 As InputStream
If hex.Contains("FFD8FF") Then ' jpg, jpeg
InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
End If
If hex.Contains("424D") Then ' bmp
InputStream1.InitializeFromBytesArray(Buffer, 8, Buffer.Length)
End If
If hex.Contains("47494638") Then ' gif
InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
End If
If hex.Contains("4D4D002A") Or hex.Contains("49492A00") Then ' tiff
InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
End If
If hex.Contains("89504E470D0A1A0A") Then ' png
InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
End If
Dim bmp As Bitmap
bmp.Initialize2(InputStream1)
InputStream1.Close
ivphotodialog.Bitmap = bmp
ivphotodialog.Gravity = Gravity.FILL
pnl.AddView(ivphotodialog,0dip,0dip,340dip,490dip)
Dim rsub1 As ResumableSub = photodialog.ShowCustom(pnl, "OK", "", "")
Wait For (rsub1) Complete (Result As Int)
If Result = xui.dialogResponse_Positive Then
'
End If
Catch
Log(LastException)
xui.MsgboxAsync("Can not load bitmap image","Bitmap image")
End Try
End Sub