Android Question Best technique to set and get a special type var into combo?

netsistemas

Active Member
Licensed User
Longtime User
what do you think is the best technique to vinculate a special data type into a combo?

thats is. Sample:
. in a combo a put a name of customer, but i need all fields about cuscomer: name, code, tax, phone, etc.

when the user select a custome in combo (by name), i need return all data.

Now, i vincule a LIST, with maps with equal id to selectedindex. When the user select a item, i read in a public (or local), list with the selectedindex, and read the INDEX of map into the list.

That is not very easy to use and understand.
any good idea:
This is my actual code:

B4X:
Sub CargarComboPlus3(ComboPlusLocal As B4XComboBox, Sql As String , CampoEnCombo As String, ValorActual As String ,ListaEnMemoriaDeMapas As List ,TituloELEMENTOUno As String   )
Try
   
    If TituloELEMENTOUno = "" Then
        TituloELEMENTOUno = VALORTITULOCOMBO
    End If

   
    Dim JD As JdbcResultSet
    Dim Txt As String
    Dim Mapa As Map
    Dim ExisteValorActual As Boolean
        ExisteValorActual = False
    If ListaEnMemoriaDeMapas.IsInitialized = False Then
        JD = GetRecordSet(Sql,True)

        CloneRsToMAP(JD,ListaEnMemoriaDeMapas)
    End If
   
    Dim F As Int
    ComboPlusLocal.cmbBox.Clear
    ComboPlusLocal.cmbBox.Add(TituloELEMENTOUno)
    ComboPlusLocal.SelectedIndex = 0
    For f = 0 To ListaEnMemoriaDeMapas.Size - 1
        Dim Mapa As Map
        Mapa =     ListaEnMemoriaDeMapas.Get(f)
        Txt = Mapa.Get(CampoEnCombo)
       
        ComboPlusLocal.cmbBox.Add(Txt)
        If ValorActual = Txt  Then
            ComboPlusLocal.cmbBox.SelectedIndex  = ComboPlusLocal.cmbBox.Size -1
            ExisteValorActual = True
        End If
        'Loop
    Next

       
       
    Catch
        Log(LastException)
    End Try
   

End Sub

and in combo select;
B4X:
Private Sub B4XTipoMedida_SelectedIndexChanged (Index As Int)
    Dim Mapa As Map
    Dim InputType As String
    Try
       
    Mapa =LTiposMedidas.Get(Index-1)
    B4XTipoUnidades.Text = Mapa.Get("Medida")
    InputType = Mapa.Get("InputType")
   
    Dim IT As EditText
   
    IT =    B4XValorMedido.TextField
    Select Case InputType
        Case "TXT"
            IT.InputType=  IT.INPUT_TYPE_TEXT
        Case "INT"
            IT.InputType=  IT.INPUT_TYPE_NUMBERS
        Case "DOU"
            IT.InputType=  IT.INPUT_TYPE_DECIMAL_NUMBERS
        Case Else
            IT.InputType=  IT.INPUT_TYPE_TEXT
           
    End Select
       
    Catch
        Log(LastException)
    End Try
   
End Sub

note this code are not operative with other code.
 
Last edited:

netsistemas

Active Member
Licensed User
Longtime User
thanks, i view that is similar to my code, BUT best... embebed in a control.

i put my code in a class for best use for me (i can't change my code. I use SQL SERVER connect but someone can use)-

B4X:
Sub Class_Globals
    Dim ListaAsociada As List
    Dim Combo As B4XComboBox
    Dim MapaAsociado As Map
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(ComboAsociado As B4XComboBox,SpOSql As String,CampoVisible As String, ValorVisible As String, ValorCero As String   )
    Combo = ComboAsociado
    CN.CargarComboPlus3(Combo,SpOSql, CampoVisible,ValorVisible,ListaAsociada,ValorCero)
End Sub

public Sub ElementoSeleccionado(Index As Long) As Boolean
   
    Dim HayMapa  As Boolean

    Try
        If ListaAsociada.Size >= Index And Index <> 0 Then
            MapaAsociado =ListaAsociada.Get(Index-1)
            HayMapa  = True
        Else
            MapaAsociado = Null
            HayMapa  = False
        End If
       
    Catch
        Log(LastException)
    End Try
    Return HayMapa
   
End Sub

public Sub GetValorMapaAsociado(Campo As String ) As String
    If AreMapVinculated Then
        Return MapaAsociado.Get(Campo)
    Else
        Return ""
    End If
End Sub

private Sub AreMapVinculated As Boolean
    Dim V As Boolean
    Try
           
        v = False
        If MapaAsociado.IsInitialized Then
            If MapaAsociado <> Null Then
                v= True
            End If
        End If
    Catch
        Log(LastException)
        v = False
    End Try
   
    Return v
End Sub

public Sub GetMapaCompleto As String
    Dim Txt As String
    Dim F As Long
    Txt = ""
    Try
        If AreMapVinculated Then
            For f = 0 To MapaAsociado.Size-1
                If f <> 0 Then
                    Txt = Txt & CRLF
                   
                End If
                If MapaAsociado.GetValueAt(F) <> "" Then
                    Txt = Txt & MapaAsociado.GetKeyAt(F) & ": " & MapaAsociado.GetValueAt(F)
                End If
            Next
        End If
   

    Catch
        Log(LastException)
       
    End Try
    Return Txt
End Sub


public Sub SetValorenCombo(TextoVisible As String)
    'Dim L As Map
    Dim F As Long
    Dim ValorEnLista As String
   
    'un combo puede tener mas elementos, que el nº de elementos asoicados a la lista.
    'un valor que no existe en la lista, PERO SÍ en el combo, es cargado al final del combo, PERO NO AÑADIDO A LA LISTA DE mapas.
   
   
    Try
        Dim ExisteValor As Boolean
               
        For f = 0 To Combo.cmbBox.Size -1
            ValorEnLista = Combo.cmbBox.GetItem(F)
            If ValorEnLista = TextoVisible Then
                Combo.SelectedIndex = f
                ExisteValor = True
                If f > ListaAsociada.Size  Then
                    MapaAsociado = Null
                Else
                    MapaAsociado = ListaAsociada.Get(F-1)
                End If
               
                Exit
            End If
           
        Next
        If ExisteValor = False Then
            'no existe el valor, así que lo añado al combo. PERO OJO... QUE AL SELECCIONARLO, NO EXISTIRÁ EL VALOR ASOICADO A LA LISTA
            If TextoVisible  ="" Then
                Combo.SelectedIndex = 0
               
            Else
                   
           
                Combo.cmbBox.Add(TextoVisible)
                'Combo.cmbBox.SelectedIndex = Combo.cmbBox.Size -1
                Combo.SelectedIndex = Combo.Size -1
            End If
           
            MapaAsociado = Null
        End If
       
       
       
    Catch
        Log(LastException)
    End Try
End Sub

and in activty:

B4X:
Sub Globals
    
    Dim IClsCombo As clsCombo
'    ...
END SUB

Private Sub B4XTipoMedida_SelectedIndexChanged (Index As Int)
    Try
        

        If IClsCombo.ElementoSeleccionado    (Index) Then
            Dim InputType As String
            B4XTipoUnidades.Text = IClsCombo.GetValorMapaasociado("Medida")
            InputType = IClsCombo.GetValorMapaasociado("InputType")
''.....''

    Catch
        Log(LastException)
    End Try
End Sub


private Sub SetValorCombo(Valor As String)
    Try
        IClsCombo.SetValorenCombo(Valor)
        lblDetalleMedida.Text = IClsCombo.GetMapaCompleto
    Catch
        Log(LastException)
    End Try
    
End Sub


private Sub CargarB4XTipoMedida
    IClsCombo.Initialize( B4XTipoMedida,"SP_Partes_VerificacionTipoMedidasGet", "Medida",B4XTipoUnidades.Text,"<Tipo>")
    'CN.CargarComboPlus3(B4XTipoMedida,"SP_Partes_VerificacionTipoMedidasGet", "Medida",B4XTipoUnidades.Text,TipoMedidaLista,"<Tipo>")
End Sub

previous code use:

B4X:
Sub CargarComboPlus3(ComboPlusLocal As B4XComboBox, Sql As String , CampoEnCombo As String, ValorActual As String ,ListaEnMemoriaDeMapas As List ,TituloELEMENTOUno As String   )
Try
    
    If TituloELEMENTOUno = "" Then
        TituloELEMENTOUno = VALORTITULOCOMBO
    End If
    
    
    Dim JD As JdbcResultSet
    Dim Txt As String
    Dim Mapa As Map
    Dim ExisteValorActual As Boolean
    ExisteValorActual = False
    
    If ListaEnMemoriaDeMapas.IsInitialized = False Then
        JD = GetRecordSet(Sql,True)

        CloneRsToMAP(JD,ListaEnMemoriaDeMapas)
    End If
    
    Dim F As Int
    ComboPlusLocal.cmbBox.Clear
    ComboPlusLocal.cmbBox.Add(TituloELEMENTOUno)
    ComboPlusLocal.SelectedIndex = 0
    For f = 0 To ListaEnMemoriaDeMapas.Size - 1
        Dim Mapa As Map
        Mapa =     ListaEnMemoriaDeMapas.Get(f)
        Txt = Mapa.Get(CampoEnCombo)
        
        ComboPlusLocal.cmbBox.Add(Txt)
        If ValorActual = Txt  Then
            'ComboPlusLocal.cmbBox.SelectedIndex  = ComboPlusLocal.cmbBox.Size -1
            ComboPlusLocal.SelectedIndex  = ComboPlusLocal.Size -1
            ExisteValorActual = True
        End If
        'Loop
    Next
        
    If ExisteValorActual = False Then
        If ValorActual = "" Then
            'ComboPlusLocal.cmbBox.SelectedIndex = 0
            ComboPlusLocal.SelectedIndex = 0
        Else
            ComboPlusLocal.cmbBox.Add(ValorActual)
            'ComboPlusLocal.cmbBox.SelectedIndex  = ComboPlusLocal.cmbBox.Size -1
            ComboPlusLocal.SelectedIndex  = ComboPlusLocal.Size -1
        End If
    End If
        
        
    Catch
        Log(LastException)
    End Try
    

End Sub



Sub  GetRecordSet(SQL As String,OnWEB As Boolean) As JdbcResultSet
    Dim Rs As JdbcResultSet

'    Log(SQL)
    Try

        
        Rs = SQLOpenConection(OnWEB).ExecQuery(SQL  )
        'rav conclose SQLOpenConection(OnWEB).Close 'getrecordset
        Return Rs
    Catch
        
        Log(LastException)
        MiMsg2(LastException.Message,"Error SQLGetRecordset. Posible falta conexión")
        Return Null
    End Try
 
End Sub


private  Sub CloneRsToMAP(Rs As JdbcResultSet, ListaDeMapa As List )
    ListaDeMapa.Initialize
    Do While Rs.NextRow
        Dim Mapa As Map
        Mapa.Initialize
        
        For f  = 0 To Rs.ColumnCount-1
            Mapa.Put(Rs.GetColumnName(f),Rs.GetString2(f))
        Next
        ListaDeMapa.Add(Mapa)
    Loop

End Sub

but you need change all code for your purpouse.
This is only a idea.

BEST Use previous control referenced
 
Upvote 0
Top