Android Code Snippet ExecuteSpinnerMap

Subname: ExecuteSpinnerMap

Description:
Similar to ExecuteSpinner (see DBUtils).
Returns (and sets into the spinner tag) a map which has:
key = DescriptionFieldName - the field shown in the spinner
value = IDFieldName - the ID (int) which should be the Primary Key.

It should be added to the module DBUtils, as it uses its function ExecuteMemoryTable.

B4X:
' Similar to ExecuteSpinner (see DBUtils).
' Returns (and sets into the spinner tag) a map which has:
' key = DescriptionFieldName - the field shown in the spinner
' value = IDFieldName - the ID (int) which should be the Primary Key.
Sub ExecuteSpinnerMap(SQL As SQL, _
                      spn As Spinner, _
                      Clear As Boolean, _
                      tbl As String, _
                      IDFieldName As String, _
                      DescriptionFieldName As String, _
                      CaseSensitive As Boolean, _
                      OrderBy As Boolean) As Map

    Dim SQLString As String
    SQLString = "SELECT " & IDFieldName & "," & DescriptionFieldName & " FROM " & tbl

    If OrderBy = True Then
        If CaseSensitive = True Then
            SQLString = SQLString & " ORDER BY UPPER(" & DescriptionFieldName & ")"
        Else
            SQLString = SQLString & " ORDER BY " & DescriptionFieldName
        End If
    End If

    If Clear = True Then
        spn.Clear
    End If

    Dim Table As List
    Dim mapDescrID As Map    : mapDescrID.Initialize
    Table = ExecuteMemoryTable(SQL, SQLString, Null, 0)
    If Table.IsInitialized Then
        Dim Cols() As String
        For i = 0 To Table.Size - 1
            Cols = Table.Get(i)
            spn.Add(Cols(1))
            mapDescrID.Put(Cols(1), Cols(0))
        Next
        spn.Tag = mapDescrID
    End If

    Return mapDescrID
End Sub

Then you can use:
B4X:
Sub GetIDFromSpinnerMap(Spn As Spinner) As Int
    Dim SpinnerMap As Map
    SpinnerMap = Spn.Tag
    Try
        Return SpinnerMap.GetValueAt(Spn.SelectedIndex)
    Catch
        Return -1
    End Try
End Sub

to get the ID related to the voice selected in the spinner.


Tags: Spinner, DBUtils, SQLite, Database
 
Last edited:
Top