Android Question How to place the selected value in Spinner ?

Pravee7094

Active Member
Hi all,
In our project, we just display the four option in the spinner. But we save One character instead of full name in the database.
Now If I want to edit click the edit button go to edit activity and shows all the fields includes spinner. All the fields fetch data from the database and placed correctly.
In spinner field, I can able to fetch that one character from database.
But I don't want to place that One character. I want show the four option again in spinner and especially the before I clicked name place in the spinner.

How do I do that? Any suggestion and help?

Thank you!
 

Mashiane

Expert
Licensed User
Longtime User
I think I do get what the PO means, perhaps use a key-value pair.

1. The items you want to show in the spinner, define them as a map

B4X:
Dim items As Map
items.Initialize
items.put("--Nothing Selected--", "-")
items.put("A first item", "a")
items.put("B second item", "b")
items.put("C third item", "c")

2. Add the items to the Spinner
B4X:
For each item as string in items.Keys
cboSpinner.Add(item)
next

3. Store the items in the tag of the spinner
B4X:
cboSpinner.Tag = items

4. Set the index to zero (when you initialy read content from db)
B4X:
cboSpinner.SelectedIndex = 0

5. Get selected key of value, assuming that in the db you want to store "c", you want "C third item" to appear on the spinner

B4X:
'get the selected topic from combo
Sub GetSelectedItem As String
    'get the selected item
    Dim value As String = cboSpinner.SelectedItem
    items = cboSpinner.Tag
    If items.IsInitialized Then
        If items.ContainsKey(value) Then
            Dim key As String = items.Get(value)
            Return key
        Else
            Return ""
        End If
    Else
        Return ""
    End If   
End Sub

This will return "c" for "C third item")

usage..

B4X:
dim myselection = GetSelectedItem


6. Now the value has been stored in the db, you retrieve it and then want to update the spinner, so then you want to do the inverse of GetSelectedItem, you want to SetSelectedItem or whatever.

Lets say the key is now "a" from the db.
B4X:
Sub SetSpinnerByID(key As String)
    If key = "" Then
        cboSpinner.SelectedIndex = 0
        Return
    End If
     'read the tag values
    items = cboSpinner.Tag
    If items.IsInitialized Then
        For Each k As String In items.Keys
            Dim v As String = items.Get(k)
            If key = v Then
                'get the index
                Dim idx As Int = cboSpinner.IndexOf(k)
                cboSpinner.SelectedIndex = idx
                Exit
            End If
        Next
    End If   
End Sub

All the best!
 
Upvote 0

Pravee7094

Active Member
I think I do get what the PO means, perhaps use a key-value pair.

1. The items you want to show in the spinner, define them as a map

B4X:
Dim items As Map
items.Initialize
items.put("--Nothing Selected--", "-")
items.put("A first item", "a")
items.put("B second item", "b")
items.put("C third item", "c")

2. Add the items to the Spinner
B4X:
For each item as string in items.Keys
cboSpinner.Add(item)
next

3. Store the items in the tag of the spinner
B4X:
cboSpinner.Tag = items

4. Set the index to zero (when you initialy read content from db)
B4X:
cboSpinner.SelectedIndex = 0

5. Get selected key of value, assuming that in the db you want to store "c", you want "C third item" to appear on the spinner

B4X:
'get the selected topic from combo
Sub GetSelectedItem As String
    'get the selected item
    Dim value As String = cboSpinner.SelectedItem
    items = cboSpinner.Tag
    If items.IsInitialized Then
        If items.ContainsKey(value) Then
            Dim key As String = items.Get(value)
            Return key
        Else
            Return ""
        End If
    Else
        Return ""
    End If  
End Sub

This will return "c" for "C third item")

usage..

B4X:
dim myselection = GetSelectedItem


6. Now the value has been stored in the db, you retrieve it and then want to update the spinner, so then you want to do the inverse of GetSelectedItem, you want to SetSelectedItem or whatever.

Lets say the key is now "a" from the db.
B4X:
Sub SetSpinnerByID(key As String)
    If key = "" Then
        cboSpinner.SelectedIndex = 0
        Return
    End If
     'read the tag values
    items = cboSpinner.Tag
    If items.IsInitialized Then
        For Each k As String In items.Keys
            Dim v As String = items.Get(k)
            If key = v Then
                'get the index
                Dim idx As Int = cboSpinner.IndexOf(k)
                cboSpinner.SelectedIndex = idx
                Exit
            End If
        Next
    End If  
End Sub

All the best!
excellent Sir, Thank you so much
 
Upvote 0
Top