Spinner itemclick called by buttonclick ??

ljcomp

Member
Licensed User
Longtime User
Hi all
I have the start or an app where 1 activity has a spinner, 2 edit text and 3 buttons.
The spinner is loaded with the contents of a database and the 2 edittext are populated by 2 cols of the database.
The spinner itemclick loads the database row col2 and col3 into the edittexts.
The user can edit the edittext and save as a new record in the database, or can delete the spinner selected record.

My problem is after hitting the save button "TreeBut1" the activity crashes because the activity steps into the spinneritemclick sub ?

Can anyone see what I may have done wrong.

B4X:
'Activity module
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
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 Treespin1 As Spinner
   Dim TreeText1 As EditText
   Dim TreeText2 As EditText
   Dim pos As Int
   Dim treespinmap As Map
   Dim treeid As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("trees")
If FirstTime Then
        SQL1.Initialize(File.DirDefaultExternal, "trees1.db", True)
    End If
   Treespin1.Clear
   treespinmap.Initialize
TreeText1.Text = ""
TreeText2.Text = ""
Dim TreeCur1 As Cursor
Dim rowcount As Int
rowcount = SQL1.ExecQuerySingleResult("Select count(*) FROM tree")
If rowcount > 0 Then
   TreeCur1 = SQL1.ExecQuery("SELECT * FROM tree")
   For i = 0 To TreeCur1.rowcount - 1
       TreeCur1.Position = i
    Treespin1.Add(TreeCur1.GetString("common")& "  :  " & TreeCur1.GetString("botanical"))
     treespinmap.Put(TreeCur1.GetString("common")& "  :  " & TreeCur1.GetString("botanical"),TreeCur1.GetString("id"))
    Next
    TreeCur1.Close
Dim TreeCur2 As Cursor
   TreeCur2 = SQL1.ExecQuery("SELECT * FROM tree")
   TreeCur2.Position = 0
      TreeText1.Text = TreeCur2.GetString("common")
      TreeText2.Text = TreeCur2.GetString("botanical")
   TreeCur2.Close
   End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub Treespin1_ItemClick (Position As Int, Value As Object)
Dim typelook As String

   Dim treecur3 As Cursor 
   'typelook = Treespin1.SelectedItem
   treeid = treespinmap.Get(Value)
   treecur3 = SQL1.ExecQuery("SELECT * From tree WHERE tree.ID = '" & treeid & "'")
treecur3.Position = 0
      TreeText1.Text = treecur3.GetString("common")
      TreeText2.Text = treecur3.GetString("botanical")
      treecur3.Close   
End Sub
Sub TreeBut3_Click
'DELETE RECORD
Dim type_1 As String

Answ=Msgbox2("Do you really want to delete the selected Tree","A T T E N T I O N","Yes","","No",Null)
   If Answ=DialogResponse.POSITIVE Then
SQL1.ExecNonQuery("DELETE FROM tree WHERE tree.ID = '" & treeid & "'")
End If
Treespin1.Clear
   treespinmap.Initialize
TreeText1.Text = ""
TreeText2.Text = ""
Dim TreeCur1 As Cursor
Dim rowcount As Int
rowcount = SQL1.ExecQuerySingleResult("Select count(*) FROM tree")
If rowcount > 0 Then
   TreeCur1 = SQL1.ExecQuery("SELECT * FROM tree")
   For i = 0 To TreeCur1.rowcount - 1
       TreeCur1.Position = i
    Treespin1.Add(TreeCur1.GetString("common")& "  :  " & TreeCur1.GetString("botanical"))
     treespinmap.Put(TreeCur1.GetString("common")& "  :  " & TreeCur1.GetString("botanical"),TreeCur1.GetString("id"))
    Next
    TreeCur1.Close
Dim TreeCur2 As Cursor
   TreeCur2 = SQL1.ExecQuery("SELECT * FROM tree")
   TreeCur2.Position = 0
      TreeText1.Text = TreeCur2.GetString("common")
      TreeText2.Text = TreeCur2.GetString("botanical")
   TreeCur2.Close
   End If
End Sub
Sub TreeBut2_Click
TreeText1.Text = ""
TreeText2.Text = ""
End Sub
Sub TreeBut1_Click
'SAVE TO DATABAE
SQL1.ExecNonQuery2("INSERT INTO tree VALUES(NULL, ?, ?)", Array As Object(TreeText1.Text, TreeText2.text ))
Msgbox(SQL1.ExecQuerySingleResult("SELECT count(*) FROM tree"),"Count")
Treespin1.Clear
TreeText1.Text = ""
TreeText2.Text = ""
Dim treecur1 As Cursor
Dim rowcount As Int
rowcount = SQL1.ExecQuerySingleResult("Select count(*) FROM tree")
If rowcount > 0 Then
   treecur1 = SQL1.ExecQuery("SELECT * FROM tree")
   For i = 0 To treecur1.rowcount - 1
       treecur1.Position = i
    Treespin1.Add(treecur1.GetString("common"))
   Treespin1_ItemClick(1, Treespin1.GetItem(0))
     Next
    treecur1.Close
Dim treecur2 As Cursor
   treecur2 = SQL1.ExecQuery("SELECT * FROM tree")
   treecur2.Position = 0
      TreeText1.Text = treecur2.GetString("common")
      TreeText2.Text = treecur2.GetString("botanical")
   treecur2.Close
   End If   
End Sub

Many thanks
LJ
 

ljcomp

Member
Licensed User
Longtime User
I thought I could stop the crash by testing treeid <>Null but the program steps to
B4X:
TreeText1.Text = treecur3.GetString("common")
Even though treeid is Null ?

B4X:
Sub Treespin1_ItemClick (Position As Int, Value As Object)
Dim typelook As String
Dim treecur3 As Cursor
   'typelook = Treespin1.SelectedItem
   treeid = treespinmap.Get(Value)
   If treeid <> Null Then
   treecur3 = SQL1.ExecQuery("SELECT * From tree WHERE tree.ID = '" & treeid & "'")
treecur3.Position = 0
      TreeText1.Text = treecur3.GetString("common")
      TreeText2.Text = treecur3.GetString("botanical")
      treecur3.Close
   End If
End Sub

shouldn't the code step from the if statement to the end if, if treeid = Null

LJ
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You must move this line:
B4X:
Treespin1_ItemClick(1, Treespin1.GetItem(0))
after the Next line.
With Treespin1_ItemClick(1... you try to raise the event for item 1, but when i = 0, item 1 doesn't exist yet.
As you call Treespin1_ItemClick you could remove the last lines.

Best regards.
 
Upvote 0
Top