It is required to return the value "i" of each selected entry by SearchView1_ItemClick.
B4X:
Private m As Map
m.Initialize
Dim lst As List
lst.Initialize
For i=0 To 5
m.Put(i,"name")
Next
For i=0 To m.Size-1
lst.Add(m.GetValueAt(i))
Next
index = SearchView1.SetItems(lst)
You could change the code inside SearchView to do what you want. for example:
B4X:
'Alter SearchView to return the index instead of the value
Private Sub lv_ItemClick (Position As Int, Value As Object)
et.Text = Value
et.SelectionStart = et.Text.Length
ime.HideKeyboard
lv.Visible = False
If SubExists(mCallback, mEventName & "_ItemClick") Then
CallSub2(mCallback, mEventName & "_ItemClick", Index)
End If
End Sub
Then you can use the map you created.
B4X:
Sub SearchView1_ItemClick(index as int)
MsgboxAsync("Chosen index: " & Index&CRLF&"Chosen value is "&m.get(i), "")
End Sub
In your code there is no need to use getValueat as your keys are integers so get will work.
This complete example uses B4XSearchTemplate (requires Xui Views lib checked). If you want to use it instead of the old SearchView:
B4X:
Sub Globals
Private Dialog As B4XDialog
Private XUI As XUI
Private SearchTemplate As B4XSearchTemplate 'need XUI VIEWS lib
End Sub
Sub Activity_Create(FirstTime As Boolean) 'ignore
Dialog.Initialize (Activity) 'you can use B4XPages too
SearchTemplate.Initialize
Private m As Map
m.Initialize
Dim lst As List
lst.Initialize
For i=0 To 5
m.Put(i,"name" &i)
Next
For Each k As Int In m.Keys
lst.Add(k)
Log($"${k} ${m.Get(k)}"$)
Next
SearchTemplate.SetItems(lst)
Wait For (Dialog.ShowTemplate(SearchTemplate, "", "", "CANCEL")) Complete (Result As Int)
If Result = XUI.DialogResponse_Positive Then
Log(SearchTemplate.SelectedItem)
End If
End Sub
I have attached the original SearchView example, in which I am trying to return the sequence number in the cities list.
Please look at the code and, if possible, suggest the correct solution.
I have attached the original SearchView example, in which I am trying to return the sequence number in the cities list.
Please look at the code and, if possible, suggest the correct solution.
You don't need all of that Cities stuff in the SearchView
Keep the cities in a global variable in Main
All you need to do is to change this sub in searchview
B4X:
Private Sub lv_ItemClick (Position As Int, Value As Object)
et.Text = Value
et.SelectionStart = et.Text.Length
ime.HideKeyboard
lv.Visible = False
If SubExists(mCallback, mEventName & "_ItemClick") Then
' Don't return the value CallSub2(mCallback, mEventName & "_ItemClick", Value)
' Return the index
CallSub2(mCallback, mEventName & "_ItemClick", Position)
End If
End Sub
Now the Itemclick event has the index not the value
B4X:
Sub SearchView1_ItemClick(Position as int)
Log("Chosen index: " & Position)
End Sub
if you had cities as a global variable, you can then do this instead
B4X:
Sub SearchView1_ItemClick(Position as int)
Log("Chosen index: " & Position)
log($Chosen city is: ${cities.get(Position)}")
End Sub
It works if all items in the list are unique, but if for instance you have Moscow the one in Russia and another Moscow in America, one of them is bumped up unless you make one start with a lower case and the other with upper case.
There is another solution if you are interested: If it is a concern to you, and you have 2 items the same, you can capitalize the first letter of one and lower case the first letter of the other. This way you have no duplicates for your map. If you need help with it, come back