Android Question ID in SearchView

Sergey_New

Well-Known Member
Licensed User
Longtime User
How can I pass the ID of each entry to SearchView? The records is not unique.
 

Sergey_New

Well-Known Member
Licensed User
Longtime User
You need to provide some more information and some sample code for anyone to understand what your question is.
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)
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Looking at the code for SearchView, you can't

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.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Please look at the attached example. I can't do anything.
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
If this is not what you want, keep asking.
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Please look at the attached example. I can't do anything.
Pay attention to the warnings in your log.

Main - 47: Parameter name cannot hide global variable name. The recommended value for android:targetSdkVersion is 33 (manifest editor). (warning #31)

also this is wrong.

Log(m.get(m.get(index)))

try this instead.
Sub SearchView1_ItemClick(i As Int) Log(m.get(i)) End Sub
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
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.
 

Attachments

  • SearchView1.zip
    40.7 KB · Views: 127
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
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'm not on my development machine, but...

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
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
My head became clearer in the morning :)
Thank you all for your participation!
 

Attachments

  • SearchView1.zip
    40.6 KB · Views: 151
Upvote 0
Solution

Mahares

Expert
Licensed User
Longtime User
My head became clearer in the morning
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.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I understand that. But I didn't find any other solution
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
 
Upvote 0
Top