Android Question Have NULL as first element of an spinner loaded with SQL

FERNANDO SILVEIRA

Active Member
Licensed User
Hello Guys,

I'm loading a spinner from SQL database but I'd like to have the very 1st spinner element as NULL (or space) and give the chance the user to pick up NULL if none of the existing values applies to him.

What is the best aproach? To have DBUtils.ExecuteSpinner load spinner and then FOR/NEXT loop to shift all elements by 1 and set NULL to the list(0)?

Is there a smarter way of doing this?

Regards,
Fernando
 

Mahares

Expert
Licensed User
Longtime User
1st spinner element as NULL (or space) and give the chance the user to pick
In this case add the first element of the spinner as a space, followed by the cursor resultset to continue to populate the spinner.
B4X:
Dim spn As Spinner
    Dim txt As String
    Dim cursor1 As Cursor
    spn.Add(" ")  '1st spinner record
    txt="SELECT COUNTRY FROM mytable ORDER BY COUNTRY"
    cursor1=SQL1.ExecQuery(txt)
    For i=0 To cursor1.RowCount-1
        cursor1.Position=i
        spn.Add(cursor1.GetString("COUNTRY") )
    Next
    cursor1.close
 
Upvote 0

FERNANDO SILVEIRA

Active Member
Licensed User
In this case add the first element of the spinner as a space, followed by the cursor resultset to continue to populate the spinner.
B4X:
Dim spn As Spinner
    Dim txt As String
    Dim cursor1 As Cursor
    spn.Add(" ")  '1st spinner record
    txt="SELECT COUNTRY FROM mytable ORDER BY COUNTRY"
    cursor1=SQL1.ExecQuery(txt)
    For i=0 To cursor1.RowCount-1
        cursor1.Position=i
        spn.Add(cursor1.GetString("COUNTRY") )
    Next
    cursor1.close


Thanx, Mahares, but I took the liberty of changing DBUtils.ExecuteSpinner instead and added a parameter named FIRSTNULL, keeping evething else untouched. See new code below:

B4X:
'Executes the query and fills the Spinner with the values in the first column
Sub ExecuteSpinner(SQL As SQL, Query As String, StringArgs() As String, Limit As Int, Spinner1 As Spinner, firstNull As Boolean)
    Spinner1.Clear
    If firstNull Then Spinner1.Add(" ")    ' <== Fernando Silveira: Leave 1st element EMPTY
    Dim Table As List
    Table = ExecuteMemoryTable(SQL, Query, StringArgs, Limit)
    Dim Cols() As String
    For i = 0 To Table.Size - 1
        Cols = Table.Get(i)
        Spinner1.Add(Cols(0))
    Next
End Sub
 
Upvote 0

udg

Expert
Licensed User
Longtime User
IMHO, modifying a "standard" class is generally not a good idea. Think of when an update will be available..you'll have to reply all the modifications you did on its previous version.
 
Upvote 0
Top