Android Tutorial ScrollView example with multiselection and SQL

Here is another ScrollView example with:
- adding a row
- moving a selected row up and down
- mono-selection, clicking on one row selects it, clicking on another row unselects the previous one and selects the new one.
- multi-selection, after one row is selected, a longclick on another row activates multi-selection, clicking on a third row selects it, clicking on a selected row unselects it. Unselecting down to one selected row reactivates mono-selection.
- deleting rows, mono and multi

Best regards.
 

Attachments

  • ScrollViewMultiselect.zip
    9.5 KB · Views: 4,056
  • ScrollViewMultiselect1.jpg
    ScrollViewMultiselect1.jpg
    48.4 KB · Views: 20,605
  • ScrollViewMultiselect2.jpg
    ScrollViewMultiselect2.jpg
    47.9 KB · Views: 20,058

klaus

Expert
Licensed User
Longtime User
Ansewer to this post:
no I havent seen this example, so this is what I was talking about when saying the resources are fragmented and not easy to find. Looks like this is what I was looking for. Thank you (soon you become my hero;-)

PS>
just run your App and I can't seem to figure out what the Modif button supposed to do ?
It would be also great to add some recordEdit form to change existing record.

Art

just run your App and I can't seem to figure out what the Modif button supposed to do ?
It would be also great to add some recordEdit form to change existing record.
You should have a closer look at the program and you will find the answer on your own. When you have found the answer to the first question you will have an aswer for the second one.

no I havent seen this example, so this is what I was talking about when saying the resources are fragmented and not easy to find. Looks like this is what I was looking for.
I just tested the search function in the top right corner, with 'sql example'. And guess what example came out first ? The example I suggested you.

Best regards.
 

blexnmedia

Member
Licensed User
Longtime User
Remote DB Connection

Hello Klaus,

In your examples, a local db is being created and used. Do you have any sample of the ScrollView but being filled with data from a remote MySQL DB?

Thanks.
 

Hodi

Member
Licensed User
Longtime User
Hi Klaus (and all other:),



I worked a little bit with your code to learn more about the scrollview.
I want to add a edittext external of the scrollview and want to multiselect all rows where the edittext is found in scrollview.

I´ve add edittext and tried to do the same like the Cell_longclick :

Sub Edittext1_EnterPressed
Cursor1 = SQL1.ExecQuery("SELECT * FROM Table WHERE col1= '" & Edittext1.Text & "'")

Dim row(NumberOfColumns) As String
row(0)="col0"
row(1)="col1"
row(2)="col2"
NumberOfRows=0
For i = 0 To Cursor1.RowCount - 1
Dim row(NumberOfColumns) As String
Cursor1.Position = i
row(0)=Cursor1.GetString("col0")
row(1)=Cursor1.GetString("col1")
row(2)=Cursor1.GetString("col2")
Next
Cursor1.Close
If Cursor1.RowCount = 0 Then
Msgbox ("text don´t match...")
Else
If SelectedItems.Size=1 Then
Dim rc As RowCol
Dim l As Label
l = Sender
rc = l.Tag
SelectRow(rc.Row, rc.Col,0)
SelectAdd
Else
Cell_Click
End If
End Sub


Please can everybody can helf to show me the right way to do this?

Thanks in advance
Hodi
 

klaus

Expert
Licensed User
Longtime User
It would be easier to help you if you posted your project as a zip file with a database example.

There are some strange things in your code.
This code is for nothing because you dim row again in the loop.
B4X:
Dim row(NumberOfColumns) As String
row(0)="col0"
row(1)="col1"
row(2)="col2"
In this part of the code row(0), row(1) and row(2) will have the values of the last row.
B4X:
For i = 0 To Cursor1.RowCount - 1
   Dim row(NumberOfColumns) As String
   Cursor1.Position = i
   row(0)=Cursor1.GetString("col0")
   row(1)=Cursor1.GetString("col1")
   row(2)=Cursor1.GetString("col2")
Next
This test is too late it must be before the For/Next loop.
B4X:
If Cursor1.RowCount = 0 Then
Best regards.
 

Hodi

Member
Licensed User
Longtime User
Hi Klaus,

First, Thanks for your quick answer - and secound, Sorry I´ve copied two different parts of the code together....

The complette code ist:

Sub Process_Globals
Dim SQL1 As SQL
End Sub

Sub Globals
Dim Edittext1 As EditText
Dim Edittext3 As EditText
Dim Edittext2 As EditText
Dim scvList As ScrollView
Dim Header As Panel
Dim Pan1 As Panel
Dim SelectedItems As List
Dim NumberOfRows As Int : NumberOfRows=0
Dim PreviousRow As Int : PreviousRow=-1
Dim NumberOfVisibleRows As Int
Dim NumberOfColumns As Int : NumberOfColumns = 3
Dim RowHeight, RowHeight_1, RowLineWidth As Int
RowLineWidth = 1dip
RowHeight = 50dip
RowHeight_1 = RowHeight - RowLineWidth

Type RowCol (Row As Int, Col As Int)
Dim LineColor As Int : LineColor = Colors.Transparent
Dim ColumnWidth(3) As Int
Dim ColumnWidth_1(3) As Int
Dim ColLineWidth As Int : ColLineWidth = 1dip
Dim SelectedRowColor As Int : SelectedRowColor=Colors.Green
Dim SelectedCellColor As Int : SelectedCellColor=Colors.Red
'Table settings
Dim HeaderColor(3) As Int
HeaderColor(0)=Colors.ARGB (120,0,0,0)
HeaderColor(1)=Colors.ARGB (120,0,0,0)
HeaderColor(2)=Colors.ARGB (180,0,0,0)

Dim HeaderFontColor(3) As Int
HeaderFontColor(0)=Colors.White
HeaderFontColor(1)=Colors.White
HeaderFontColor(2)=Colors.White

Dim CellColor(3) As Int
CellColor(0) = Colors.ARGB (30,0,0,0)
CellColor(1) = Colors.ARGB (30,0,0,0)
CellColor(2) = Colors.ARGB (30,0,0,0)
Dim FontColor(3) As Int
FontColor(0) = Colors.Black
FontColor(1) = Colors.Black
FontColor(2) = Colors.Red

Dim FontSize(3) As Float
FontSize(0) = 12
FontSize(1) = 18
FontSize(2) = 24

Dim Alignment(3) As Int
Alignment(0) = Gravity.CENTER
Alignment(1) = Gravity.CENTER
Alignment(2) = Gravity.CENTER
Dim Cursor1 As Cursor
Dim RowET As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout ("Test.bal")
Dim i As Int
scvList.Panel.Color=Colors.ARGB (120,0,0,0)
Pan1 = scvList.Panel
Pan1.Color = LineColor

ColumnWidth(0) = 0.2 * scvList.Width
ColumnWidth(1) = 0.4 * scvList.Width
ColumnWidth(2) = 0.4 * scvList.Width
For i=0 To NumberOfColumns-1

ColumnWidth_1(i) = ColumnWidth(i)-ColLineWidth
Next
SelectedItems.Initialize
If FirstTime Then
SQL1.Initialize(File.DirRootExternal,"database.db",True)
End If

NumberOfVisibleRows=scvList.Height/RowHeight
Edittext1.Hint = "check1"
Edittext2.Hint = "check2"
Edittext3.Hint = "check3"
SQL1.Initialize(File.DirRootExternal, "Database.db", True)


End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub Edittext1_EnterPressed

Cursor1 = SQL1.ExecQuery("SELECT * FROM Table1 WHERE col1= '" & Edittext1.Text & "'")

Dim row(NumberOfColumns) As String
row(0)="data1"
row(1)="data2"
row(2)="data3"
SetHeader(row)
NumberOfRows=0
For i = 0 To Cursor1.RowCount - 1
Cursor1.Position = i
row(0)=Cursor1.GetString("data1")
row(1)=Cursor1.GetString("data2")
row(2)=Cursor1.GetString("data3")

AddRow(Row)
Next

If Cursor1.RowCount = 0 Then
Msgbox ("check again", "no data found")
Else
Edittext1.Text = ""
Edittext1.Visible=False
Edittest2.Visible=True
Edittext3.Visible=False
End If
End Sub

Sub Edittext2_EnterPressed
Dim rc As RowCol
Cursor1 = SQL1.ExecQuery("SELECT * FROM Tabel1 WHERE col2= '" & Edittext2.Text & "'")
For i = 0 To Cursor1.RowCount - 1
Cursor1.Position = i
RowET=Cursor1.GetString("data2")
Next
Cursor1.Close
If Edittext2.Text = RowET Then
SelectRow(rc.Row, rc.Col,0)
selectAdd
Else
Msgbox ("data not found","Please check")
End If

End Sub

Sub Cell_Click
Dim rc As RowCol
Dim l As Label
l = Sender
rc = l.Tag

If SelectedItems.Size=1 Then
SelectRow(rc.Row, rc.Col,2)
Else
SelectRow(rc.Row, rc.Col,0)
End If
SelectAdd
End Sub

Sub Cell_LongClick
' activates multiselection
If SelectedItems.Size=1 Then
Dim rc As RowCol
Dim l As Label

l = Sender
rc = l.Tag
SelectRow(rc.Row, rc.Col,0)
SelectAdd

Else
Cell_Click
End If
End Sub

Sub SelectAdd
Dim txt As String

txt="Codes = "
For i=0 To SelectedItems.Size-1
If i=0 Then
txt=txt&GetView(SelectedItems.Get(i),0).Text
Else
txt=txt&" / "&GetView(SelectedItems.Get(i),0).Text
End If
Next
Activity.Title = txt
End Sub

Sub Header_Click
Dim l As Label
Dim col As Int
l = Sender
col = l.Tag
Activity.Title = "Header clicked: " & col
End Sub

Sub SelectRow(Row As Int, Col As Int, Mode As Int)
' Mode = 0 -> multi selection
' = 1 -> up / down move
' = 2 -> mono selection
Dim col, index As Int

index=SelectedItems.IndexOf(Row) ' tests if the row is alrady selected

If index>-1 Then ' row already selected
For col = 0 To NumberOfColumns - 1
GetView(Row, Col).Color = CellColor(col) ' remove the color of previously selected row
Next
SelectedItems.RemoveAt(index) ' removes the row from the list of selected rows
If SelectedItems.Size=1 Then
Row=SelectedItems.Get(0) ' sets row to the last selected row
End If
Else
' adds the selected row to the list and sets the line color
If Mode=0 Then
SelectedItems.Add(Row)
Else If Mode=2 Then
For col = 0 To NumberOfColumns - 1
GetView(PreviousRow, Col).Color = CellColor(col) ' sets standard colors to previous row
Next
End If
For col = 0 To NumberOfColumns - 1
GetView(Row, Col).Color = SelectedRowColor ' sets selected row colors
Next
End If

If Mode=1 Then ' up / down move
For col = 0 To NumberOfColumns - 1
GetView(PreviousRow, Col).Color = CellColor(col)
GetView(Row, Col).Color = SelectedRowColor
Next
End If

' update or the Buttons and EditText views
If SelectedItems.Size=0 Then
Else If SelectedItems.Size=1 Then
SelectedItems.Set(0,Row)
PreviousRow=Row
End If
End Sub

Sub GetView(Row As Int, Col As Int) As Label
' Returns the label in the specific cell
Dim l As Label
l = Pan1.GetView(Row * NumberOfColumns + Col)
Return l
End Sub

Sub AddRow(Values() As String)
' Adds a row to the Pan1
Dim ColWidth As Int

If values.Length <> NumberOfColumns Then
Log("Wrong number of values.")
Return
End If
ColWidth=0
For i = 0 To NumberOfColumns - 1
Dim l As Label
l.Initialize("cell")
l.Text = values(i)
l.Gravity = Alignment(i)
l.TextSize = FontSize(i)
l.TextColor = FontColor(i)
l.Color=CellColor(i)
Dim rc As RowCol
rc.Initialize
rc.Col = i
rc.Row = NumberOfRows
l.Tag = rc
Pan1.AddView(l, ColWidth, RowHeight * NumberOfRows, ColumnWidth_1(i), RowHeight_1)
ColWidth=ColWidth+ColumnWidth(i)
Next
NumberOfRows=NumberOfRows+1
Pan1.Height = NumberOfRows * RowHeight
End Sub

Sub SetHeader(Values() As String)
' Set the headers values
Dim ColWidth As Int

If header.IsInitialized Then Return 'should only be called once
header.Initialize("")
ColWidth=0
For i = 0 To NumberOfColumns - 1
Dim l As Label
l.Initialize("header")
l.Text = values(i)
l.Gravity = Alignment(i)
l.TextSize = FontSize(i)
l.Color = HeaderColor(i)
l.TextColor = HeaderFontColor(i)
l.Tag = i
header.AddView(l, ColWidth, 0, ColumnWidth_1(i), RowHeight_1)
ColWidth=ColWidth+ColumnWidth(i)
Next
Activity.AddView(header, scvList.Left, scvList.Top - RowHeight, scvList.Width, RowHeight)
End Sub

Sub SetCell(Row As Int, Col As Int, Value As String)
' Sets the value of the given cell
GetView(Row, Col).Text = value
End Sub

Sub GetCell(Row As Int, Col As Int) As String
' Gets the value of the given cell
Return GetView(Row, Col).Text
End Sub

------------------------------------------
With the Edittext2 I want to select the row If the edittext2.text is in shown in the scrollview. (the same like cell_longclick)

Please can you help me with the code? (currently the code select only the first row because I don´t know how I can call the right Row and Col)

Thanks in advance
Hodi
 

Hodi

Member
Licensed User
Longtime User
Hi Klaus,


I´ve attached the test1.zip file.
In the edittext1 please enter "1001" and in edittext2 I will select the right row like cell_longclick. (with multiselection Mode 0)

Thanks
Hodi
 

Attachments

  • test1.zip
    9.3 KB · Views: 544

StarinschiAndrei

Active Member
Licensed User
Longtime User
wrong selected item

Hi Klaus ,

I tried to modify my application based on your exemple, but it doesn't metter what cell i click the rc is (col 5 ; row 19).
I attached my app. Could you advice me how can a solve the problem ?
Thk
Sorry the zip file is bigger than the maximum alowed, for this reason i put the code here.
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim sql1 As SQL
'<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Begin - Change for Database>>>>>>>>>>>>>>>>>>>>>>>>>>
Dim DBFileName As String : DBFileName = "countries.db"
Dim DBTableName As String : DBTableName = "datePers"
'<<<<<<<<<<<<<<<<<<<<<<<<<<<<<End - Change for Database>>>>>>>>>>>>>>>>>>>>>>>>>>

' Dim DBFileDir As String : DBFileDir = File.DirInternal
Dim DBFileDir As String : DBFileDir = File.DirDefaultExternal
Dim sqltxt As String
Dim cur As Cursor
End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim spnGrupe As Spinner
Dim spnValoare As Spinner
Dim spnOre As Spinner
Dim spnstare As Spinner
Dim spnsport As Spinner
Dim lbltitlu As Label
Dim txtnume As EditText
Dim txtprenume As EditText
Dim txttelefon As EditText
Dim txtmail As EditText
Dim txtdatanastere As EditText
Dim CsCal As CustomCalendar
Dim pnlcamp As Panel
Dim l As Label

''---------------------------------------------------------------------------------
Dim nrrand As Int
Dim nrcol As Int
Dim SV As ScrollView
Dim header As Panel
Dim HeaderColor, TableColor, FontColor, HeaderFontColor As Int
Dim FontSize As Float
HeaderColor = Colors.Gray
HeaderFontColor = Colors.White
Type RowCol (Row As Int, Col As Int)
Dim SelectedRowColor As Int : SelectedRowColor=Colors.LightGray
Dim selectedItems As Map ''+18.11
Dim cellcolor As Int
'Table settings


End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")

If FirstTime Then
sql1.Initialize(DBFileDir, DBFileName, True)
End If

Activity.LoadLayout("inregistrare")
selectedItems.Initialize ''+18.11
grupe
valoare
ore
sporturi
stare
incarc_tabel
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
#Region"Populez campuri si tabel"
Sub incarc_tabel


Dim colwidth As Int
Dim rowhight As Int

rowhight=30dip
nrcol=6
Dim rand(nrcol) As String
Dim numeHeader() As String

numeHeader=Array As String("id","Nume","Prenume","NumarOre","Telefon","DataNastere")
colwidth = SV.Width /nrcol
'' adaug header
header.Initialize("")
For x = 0 To nrcol - 1
Dim lb As Label
lb.Initialize("header")
lb.Text = numeHeader(x)''numeHeader(i)

lb.Gravity = Gravity.CENTER
lb.TextSize = FontSize
lb.Color = HeaderColor
lb.TextColor = HeaderFontColor
lb.Tag = x

header.AddView(lb, colwidth * x, 0, colwidth, rowhight)
Next
Activity.AddView(header, SV.Left, SV.Top - rowhight, SV.Width, rowhight)

sqltxt="Select id,nume,prenume,nrore,telefon,datanastere from datepers"
cur=sql1.ExecQuery(sqltxt)

For y=0 To cur.RowCount -1
cur.Position =y
rand(0)=cur.GetInt("id")
rand(1)=cur.GetString("nume")
rand(2)=cur.Getstring("prenume")
rand(3)=cur.GetInt("nrOre")
rand(4)=cur.GetString("telefon")
rand(5)=cur.GetString("datanastere")
For i=0 To nrcol -1

l.Initialize("cell")
l.Gravity =Gravity.CENTER
l.TextSize =18
l.Text =rand(i)
l.TextColor =HeaderFontColor
Dim rc As RowCol
rc.Initialize
rc.Col =i
rc.Row =y
l.Tag =rc
SV.Panel.AddView(l,colwidth*i,rowhight*y,colwidth,rowhight)
Next
Next
cur.Close
SV.FullScroll(True)
SV.Panel.Height = rowhight * y
End Sub
Sub cell_click
Dim rc As RowCol
Dim l As Label
l=Sender
rc=l.Tag
If selectedItems.Size <>1 Then
selectedRow(rc.Row ,rc.Col )
End If
''Dim l As Label
'' Dim rc As RowCol

'' l = Sender
'' rc = l.Tag
'' Activity.Title = "Row " & rc.Row & " Col " & rc.Col &" / " & l.Text
End Sub
Sub selectedRow(row As Int , selcol As Int ) ''18.11
Dim col , index As Int
Msgbox(col&";"&row,"")

End Sub
Sub grupe
spnGrupe.Clear
sqltxt="select * from grupe"
cur=sql1.ExecQuery(sqltxt)
If cur.RowCount > 0 Then
spnGrupe.Add("Selecteaza Grupa")
For i=0 To cur.RowCount -1
cur.Position =i
spnGrupe.Add(cur.GetString("grupe"))
Next
End If
cur.Close
End Sub
Sub sporturi
spnsport.Clear
sqltxt = "select * from activitati"
cur=sql1.ExecQuery(sqltxt)
If cur.RowCount > 0 Then
spnsport.Add("Selecteaza Activitatea Sportiva")
For i=0 To cur.RowCount -1
cur.Position =i
spnsport.Add(cur.GetString("sport"))
Next
End If
cur.Close
End Sub
Sub valoare
spnValoare.Clear
sqltxt = "select * from valori"
cur=sql1.ExecQuery(sqltxt)
If cur.RowCount>0 Then
spnValoare.Add("Selecteaza Valoare")
For i=0 To cur.RowCount -1
cur.Position =i
spnValoare.Add(cur.GetString("valori"))
Next
spnValoare.SelectedIndex=0
End If
cur.Close
End Sub
Sub ore
spnOre.Clear
sqltxt = "select ore from nrore"
cur=sql1.ExecQuery(sqltxt)
If cur.RowCount > 0 Then
spnOre.Add("Selecteaza Numarul de ore")
For i=0 To cur.RowCount -1
cur.Position =i
spnOre.Add(cur.GetString("ore"))
Next
spnOre.SelectedIndex =0
End If
cur.Close
End Sub
Sub stare
spnstare.AddAll(Array As String("Activ","Inactiv"))
End Sub

#End Region
#Region"Functii adaugare"
Sub addDate (comanda As String)
''FPDataNastere=DateTime.DateParse(FPDataNastere)
sql1.BeginTransaction
''sql1.ExecNonQuery("Insert INTO datePers (nume,prenume,telefon,adrMail,datanastere) VALUES ('" & FPnume & "','" & FPprenume & "','" & FPtelefon & "','" & FPadrmail & "','" & FPdata & "')")
sql1.ExecNonQuery(comanda)
sql1.TransactionSuccessful
sql1.EndTransaction
End Sub
#End Region

Sub btnSalveaza_Click
Dim data As String
Dim now As Long
Dim SQL As String
Dim idClient As Int

If txtnume.Text = "Nume" Then
Msgbox("Nu ai completat campul Nume","Eroare Operare")
Return
End If
If txtprenume.Text ="Prenume" Then
Msgbox("Nu ai completat campul Prenume","Eroare Operare")
Return
End If
If txttelefon.Text ="Telefon" Then
Msgbox("Nu ai completat campul Telefon","Eroare Operare")
Return
End If
If spnGrupe.selectedItem="Selecteaza Grupa" Then
Msgbox("Nu ai selectat Grupa","Eroare Operare")
Return
End If
If spnValoare.selectedItem = "Selecteaza Valoare" AND spnOre.selectedItem<>"Selecteaza Numarul de ore" Then
Msgbox("Ai uitat sa selectezi Valoare","Eroare Operare")
Return
End If
If spnOre.selectedItem ="Selecteaza Numarul de ore" AND spnValoare.selectedItem <> "Selecteaza Valoare" Then
Msgbox("Ai uitat sa selectezi valoarea","Eroare Operare")
Return
End If
'' Inserez datele personalre
SQL="Insert INTO datePers (nume,prenume,telefon,adrMail,datanastere,grupa,sport,stare)VALUES ('" & txtnume.Text & "','" & txtprenume.Text & "','" & txttelefon.Text & "','" & txtmail.Text & "','" & txtdatanastere.Text & "','" & spnGrupe.selectedItem & "','" &spnsport.selectedItem &"','" & spnstare.selectedItem &"')"
addDate(SQL)
'' Caut ID de client
SQL="Select id from datePers where nume = '" &txtnume.Text &"'and prenume='"& txtprenume.Text &"'and telefon='" &txttelefon.Text &"'and adrMail='"&txtmail.Text &"'"

cur=sql1.ExecQuery(SQL)
For i=0 To cur.RowCount -1
cur.Position =i
idClient=cur.GetInt("id")
Next
cur.Close

''daca se face plata la inregistrare inserez in activZilnica
DateTime.DateFormat="yyyy-MM-dd"
If spnValoare.selectedItem <> "Selecteaza Valoare" Then
data=DateTime.Date(now)
SQL="Insert INTO activZilnica(idpers,data,suma)VALUES('"& idClient &"','" & data &"','" &spnValoare.selectedItem &"')"
addDate(SQL)
End If
incarc_tabel
End Sub
Sub txtTelefon_FocusChanged (HasFocus As Boolean)
If HasFocus=True AND txttelefon.Text = "Telefon" Then
txttelefon.Text =""
Else
If txttelefon.Text ="" Then
txttelefon.Text ="Telefon"
End If
End If
End Sub
Sub txtPrenume_FocusChanged (HasFocus As Boolean)
If HasFocus=True AND txtprenume.Text = "Prenume" Then
txtprenume.Text =""
Else
If txtprenume.Text ="" Then
txtprenume.Text ="Prenume"
End If
End If
End Sub
Sub txtNume_FocusChanged (HasFocus As Boolean)
If HasFocus=True AND txtnume.Text = "Nume" Then
txtnume.Text =""
Else
If txtnume.Text ="" Then
txtnume.Text ="Nume"
End If
End If
End Sub
Sub txtMail_FocusChanged (HasFocus As Boolean)
If HasFocus=True AND txtmail.Text = "Adresa Mail" Then
txtmail.Text =""
Else
If txtmail.Text ="" Then
txtmail.Text ="Adresa Mail"
End If
End If
End Sub
Sub txtDataNastere_FocusChanged (HasFocus As Boolean)

If HasFocus = True Then
CsCal.Initialize(Me,"CsCal",100%x,100%y,DateTime.Now)
Activity.AddView(CsCal.AsView,0,0,100%x,100%y)
CsCal.ShowCalendar(True)
End If
End Sub
 
Last edited:

StarinschiAndrei

Active Member
Licensed User
Longtime User
Hi Klaus ,

I tried to modify my application based on your exemple, but it doesn't metter what cell i click the rc is (col 5 ; row 19).
I attached my app. Could you advice me how can a solve the problem ?
Thk
Sorry the zip file is bigger than the maximum alowed, for this reason i put the code here.
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim sql1 As SQL
'<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Begin - Change for Database>>>>>>>>>>>>>>>>>>>>>>>>>>
Dim DBFileName As String : DBFileName = "countries.db"
Dim DBTableName As String : DBTableName = "datePers"
'<<<<<<<<<<<<<<<<<<<<<<<<<<<<<End - Change for Database>>>>>>>>>>>>>>>>>>>>>>>>>>

' Dim DBFileDir As String : DBFileDir = File.DirInternal
Dim DBFileDir As String : DBFileDir = File.DirDefaultExternal
Dim sqltxt As String
Dim cur As Cursor
End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim spnGrupe As Spinner
Dim spnValoare As Spinner
Dim spnOre As Spinner
Dim spnstare As Spinner
Dim spnsport As Spinner
Dim lbltitlu As Label
Dim txtnume As EditText
Dim txtprenume As EditText
Dim txttelefon As EditText
Dim txtmail As EditText
Dim txtdatanastere As EditText
Dim CsCal As CustomCalendar
Dim pnlcamp As Panel
Dim l As Label

''---------------------------------------------------------------------------------
Dim nrrand As Int
Dim nrcol As Int
Dim SV As ScrollView
Dim header As Panel
Dim HeaderColor, TableColor, FontColor, HeaderFontColor As Int
Dim FontSize As Float
HeaderColor = Colors.Gray
HeaderFontColor = Colors.White
Type RowCol (Row As Int, Col As Int)
Dim SelectedRowColor As Int : SelectedRowColor=Colors.LightGray
Dim selectedItems As Map ''+18.11
Dim cellcolor As Int
'Table settings


End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")

If FirstTime Then
sql1.Initialize(DBFileDir, DBFileName, True)
End If

Activity.LoadLayout("inregistrare")
selectedItems.Initialize ''+18.11
grupe
valoare
ore
sporturi
stare
incarc_tabel
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
#Region"Populez campuri si tabel"
Sub incarc_tabel


Dim colwidth As Int
Dim rowhight As Int

rowhight=30dip
nrcol=6
Dim rand(nrcol) As String
Dim numeHeader() As String

numeHeader=Array As String("id","Nume","Prenume","NumarOre","Telefon","DataNastere")
colwidth = SV.Width /nrcol
'' adaug header
header.Initialize("")
For x = 0 To nrcol - 1
Dim lb As Label
lb.Initialize("header")
lb.Text = numeHeader(x)''numeHeader(i)

lb.Gravity = Gravity.CENTER
lb.TextSize = FontSize
lb.Color = HeaderColor
lb.TextColor = HeaderFontColor
lb.Tag = x

header.AddView(lb, colwidth * x, 0, colwidth, rowhight)
Next
Activity.AddView(header, SV.Left, SV.Top - rowhight, SV.Width, rowhight)

sqltxt="Select id,nume,prenume,nrore,telefon,datanastere from datepers"
cur=sql1.ExecQuery(sqltxt)

For y=0 To cur.RowCount -1
cur.Position =y
rand(0)=cur.GetInt("id")
rand(1)=cur.GetString("nume")
rand(2)=cur.Getstring("prenume")
rand(3)=cur.GetInt("nrOre")
rand(4)=cur.GetString("telefon")
rand(5)=cur.GetString("datanastere")
For i=0 To nrcol -1

l.Initialize("cell")
l.Gravity =Gravity.CENTER
l.TextSize =18
l.Text =rand(i)
l.TextColor =HeaderFontColor
Dim rc As RowCol
rc.Initialize
rc.Col =i
rc.Row =y
l.Tag =rc
SV.Panel.AddView(l,colwidth*i,rowhight*y,colwidth,rowhight)
Next
Next
cur.Close
SV.FullScroll(True)
SV.Panel.Height = rowhight * y
End Sub
Sub cell_click
Dim rc As RowCol
Dim l As Label
l=Sender
rc=l.Tag
If selectedItems.Size <>1 Then
selectedRow(rc.Row ,rc.Col )
End If
''Dim l As Label
'' Dim rc As RowCol

'' l = Sender
'' rc = l.Tag
'' Activity.Title = "Row " & rc.Row & " Col " & rc.Col &" / " & l.Text
End Sub
Sub selectedRow(row As Int , selcol As Int ) ''18.11
Dim col , index As Int
Msgbox(col&";"&row,"")

End Sub
Sub grupe
spnGrupe.Clear
sqltxt="select * from grupe"
cur=sql1.ExecQuery(sqltxt)
If cur.RowCount > 0 Then
spnGrupe.Add("Selecteaza Grupa")
For i=0 To cur.RowCount -1
cur.Position =i
spnGrupe.Add(cur.GetString("grupe"))
Next
End If
cur.Close
End Sub
Sub sporturi
spnsport.Clear
sqltxt = "select * from activitati"
cur=sql1.ExecQuery(sqltxt)
If cur.RowCount > 0 Then
spnsport.Add("Selecteaza Activitatea Sportiva")
For i=0 To cur.RowCount -1
cur.Position =i
spnsport.Add(cur.GetString("sport"))
Next
End If
cur.Close
End Sub
Sub valoare
spnValoare.Clear
sqltxt = "select * from valori"
cur=sql1.ExecQuery(sqltxt)
If cur.RowCount>0 Then
spnValoare.Add("Selecteaza Valoare")
For i=0 To cur.RowCount -1
cur.Position =i
spnValoare.Add(cur.GetString("valori"))
Next
spnValoare.SelectedIndex=0
End If
cur.Close
End Sub
Sub ore
spnOre.Clear
sqltxt = "select ore from nrore"
cur=sql1.ExecQuery(sqltxt)
If cur.RowCount > 0 Then
spnOre.Add("Selecteaza Numarul de ore")
For i=0 To cur.RowCount -1
cur.Position =i
spnOre.Add(cur.GetString("ore"))
Next
spnOre.SelectedIndex =0
End If
cur.Close
End Sub
Sub stare
spnstare.AddAll(Array As String("Activ","Inactiv"))
End Sub

#End Region
#Region"Functii adaugare"
Sub addDate (comanda As String)
''FPDataNastere=DateTime.DateParse(FPDataNastere)
sql1.BeginTransaction
''sql1.ExecNonQuery("Insert INTO datePers (nume,prenume,telefon,adrMail,datanastere) VALUES ('" & FPnume & "','" & FPprenume & "','" & FPtelefon & "','" & FPadrmail & "','" & FPdata & "')")
sql1.ExecNonQuery(comanda)
sql1.TransactionSuccessful
sql1.EndTransaction
End Sub
#End Region

Sub btnSalveaza_Click
Dim data As String
Dim now As Long
Dim SQL As String
Dim idClient As Int

If txtnume.Text = "Nume" Then
Msgbox("Nu ai completat campul Nume","Eroare Operare")
Return
End If
If txtprenume.Text ="Prenume" Then
Msgbox("Nu ai completat campul Prenume","Eroare Operare")
Return
End If
If txttelefon.Text ="Telefon" Then
Msgbox("Nu ai completat campul Telefon","Eroare Operare")
Return
End If
If spnGrupe.selectedItem="Selecteaza Grupa" Then
Msgbox("Nu ai selectat Grupa","Eroare Operare")
Return
End If
If spnValoare.selectedItem = "Selecteaza Valoare" AND spnOre.selectedItem<>"Selecteaza Numarul de ore" Then
Msgbox("Ai uitat sa selectezi Valoare","Eroare Operare")
Return
End If
If spnOre.selectedItem ="Selecteaza Numarul de ore" AND spnValoare.selectedItem <> "Selecteaza Valoare" Then
Msgbox("Ai uitat sa selectezi valoarea","Eroare Operare")
Return
End If
'' Inserez datele personalre
SQL="Insert INTO datePers (nume,prenume,telefon,adrMail,datanastere,grupa,sport,stare)VALUES ('" & txtnume.Text & "','" & txtprenume.Text & "','" & txttelefon.Text & "','" & txtmail.Text & "','" & txtdatanastere.Text & "','" & spnGrupe.selectedItem & "','" &spnsport.selectedItem &"','" & spnstare.selectedItem &"')"
addDate(SQL)
'' Caut ID de client
SQL="Select id from datePers where nume = '" &txtnume.Text &"'and prenume='"& txtprenume.Text &"'and telefon='" &txttelefon.Text &"'and adrMail='"&txtmail.Text &"'"

cur=sql1.ExecQuery(SQL)
For i=0 To cur.RowCount -1
cur.Position =i
idClient=cur.GetInt("id")
Next
cur.Close

''daca se face plata la inregistrare inserez in activZilnica
DateTime.DateFormat="yyyy-MM-dd"
If spnValoare.selectedItem <> "Selecteaza Valoare" Then
data=DateTime.Date(now)
SQL="Insert INTO activZilnica(idpers,data,suma)VALUES('"& idClient &"','" & data &"','" &spnValoare.selectedItem &"')"
addDate(SQL)
End If
incarc_tabel
End Sub
Sub txtTelefon_FocusChanged (HasFocus As Boolean)
If HasFocus=True AND txttelefon.Text = "Telefon" Then
txttelefon.Text =""
Else
If txttelefon.Text ="" Then
txttelefon.Text ="Telefon"
End If
End If
End Sub
Sub txtPrenume_FocusChanged (HasFocus As Boolean)
If HasFocus=True AND txtprenume.Text = "Prenume" Then
txtprenume.Text =""
Else
If txtprenume.Text ="" Then
txtprenume.Text ="Prenume"
End If
End If
End Sub
Sub txtNume_FocusChanged (HasFocus As Boolean)
If HasFocus=True AND txtnume.Text = "Nume" Then
txtnume.Text =""
Else
If txtnume.Text ="" Then
txtnume.Text ="Nume"
End If
End If
End Sub
Sub txtMail_FocusChanged (HasFocus As Boolean)
If HasFocus=True AND txtmail.Text = "Adresa Mail" Then
txtmail.Text =""
Else
If txtmail.Text ="" Then
txtmail.Text ="Adresa Mail"
End If
End If
End Sub
Sub txtDataNastere_FocusChanged (HasFocus As Boolean)

If HasFocus = True Then
CsCal.Initialize(Me,"CsCal",100%x,100%y,DateTime.Now)
Activity.AddView(CsCal.AsView,0,0,100%x,100%y)
CsCal.ShowCalendar(True)
End If
End Sub

Hi Klaus , after few our investigation i solved the problem :BangHead:
 

Tornet91

Member
Licensed User
Longtime User
How can I make so the code row can't be the same as Another row ?´

Best regards
Tornet
 

Tornet91

Member
Licensed User
Longtime User
I want to make a list there user add's Article number and articlename, but if they write same article number or article name twice, it will come up a msgbox ("You have choice same article numbers","Error") And it doesnt come up a new row.
 

klaus

Expert
Licensed User
Longtime User
You should request a query to check if this entry already exists.
Something like this:
B4X:
'first we check if the entry already does exist
Query = "SELECT * FROM DatabaseName WHERE ArticleNumber = ? AND ArticleName = ?"
Cursor1 = Main.SQL1.ExecQuery2(Query, Array As String (edtArticleNumber .Text, edtArticleName.Text))

If Cursor1.RowCount > 0 Then
    'if it exists show a message and do nothing else
    ToastMessageShow("This entry already exists", False)
Else
    'if not, add the entry
    'your code
End If
 
Top