Android Question From EditText.Text to Map?

anOparator

Active Member
Licensed User
Longtime User
And there,
I'm trying to use EditText1.Text as the Key in a Map. So that different names entered into EditText1 return different Values.

The For loop works of course:
B4X:
Sub EditText12_EnterPressed         ' this works as EditText1
   Dim Map1 As Map = CreateMap("alan": 1, "robert": 2, "tony": 3)
   For Each key As String In Map1.Keys
   Log ("Key: " & key)
   Log ("Value: " & Map1.Get(key))
Next
End Sub
How would it be done using EditText1.Text as the Key?
B4X:
Sub EditText1_EnterPressed
   Dim Map1 As Map = CreateMap("alan": 1, "robert": 2, "tony": 3)

   Dim mapNme = EditText1.Text As String   ' "Invalid double" error
'   Dim mapNme = EditText1.Text As Int     ' "Invalid double" error
'     Log(Map1.GetValueAt(mapNme))
'   Log(Map1.GetValueAt(tony))
   Log("Value: " & Map1.GetValueAt(mapNme))
End Sub
And the Value returned be used as a variable:
Imageview1.Bitmap = LoadBitmap(File.DirAssets, $"${Value}. jpg"$)
Thanks in advance.
 

Attachments

  • nameMap.zip
    7.9 KB · Views: 193
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Never use GetKeyAt / GetValueAt.

Dim mapNme = EditText1.Text As String
This line will not throw an "invalid double" error.

Correct code:
B4X:
If Map1.ContainsKey(EditText1.Text) Then
Dim value As Int = Map1.Get(EditText1.Text)
End If
'or
Dim value As Int = Map1.GetDefault(EditText1.Text, 0)
 
Upvote 0
Top