Android Question Searchview return value

jayel

Active Member
Licensed User
Longtime User
Hello,

I am strungling with the searchview, as is, it works, but I have to return the index of the choosen item from the original list, so I can look in another list of maps to return the id.
How can I do that?

greets

John
 

DonManfred

Expert
Licensed User
Longtime User
How can I do that?
Change the class to fill the internal Listview not just with a
B4X:
lv.AddSingleLine(li.Get(i))
Instead you should use
B4X:
lv.AddSingleLine2(li.Get(i),value/map/whatever)
to give the value you need when the event
B4X:
Sub sv_ItemClick(Value As String)
    Msgbox("Chosen value: " & Value, "")
End Sub
is raised
 
Upvote 0

Anser

Well-Known Member
Licensed User
Longtime User
Change the class to fill the internal Listview not just with a
B4X:
lv.AddSingleLine(li.Get(i))
Instead you should use
B4X:
lv.AddSingleLine2(li.Get(i),value/map/whatever)
to give the value you need when the event
B4X:
Sub sv_ItemClick(Value As String)
    Msgbox("Chosen value: " & Value, "")
End Sub
is raised
I could not get this done. I am trying to follow what DonManfred has advised to do.

Any help will be appreciated.

My Objective:-
As jayel said. Customer Name will be displayed to the user on the SearchView, Once the user selects a Customer I need to get the CustomerCode of that particular Customer. To get this done what would be the best way to be used in SearchView. I know that this is a very common requirement for many here. Unfortunately, I can't understand which direction to move.

In my activity, I have created 2 Lists. One list will contain the CustomerCode and the other list will contain the CustomerName. My intention is to get the Position Index or something similar from SearchView so that once the user selects a CustomerName from the searchview, I could get the CustomerCode from the Activity itself without extensive modification of the SearchView class.
CustomerCode.Get(Position)

for eg
B4X:
Sub SQL_QueryComplete(Success As Boolean, Crsr As Cursor)
   If Success Then
      CustomerList.Clear
      CustomerCode.Clear
      For i = 0 To Crsr.RowCount-1
       Crsr.Position = i
      
       CustomerList.Add( Crsr.GetString("NAME") )
       CustomerCode.Add( Crsr.GetString("CUST_CODE") )
      Next
     svCustListIndex = svCustomer.SetItems(CustomerList)
   
   End If
End Sub

Sub svCustomer_ItemClick(Value As Object)
   Dim Row(2) As String
   Row = Value

   Msgbox("Chosen value: " & Row(1), Row(0))
   Msgbox(CustomerCode.Get(Row(0)),CustomerList.Get(Row(0))  )
End Sub

I made the following changes in the SearchView Class ie in the Sub lv_ItemClick and AddItemsToList
B4X:
Private Sub lv_ItemClick (Position As Int, Value As Object)
   Dim Row() As String
   Row = Value

   et.Text = Row(1)
   et.SelectionStart = et.Text.Length
   ime.HideKeyboard
   lv.Visible = False
   If SubExists(mCallback, mEventName & "_ItemClick") Then
     CallSub2(mCallback, mEventName & "_ItemClick", Value)
   End If
End Sub


Private Sub AddItemsToList(li As List, full As String)
    If li.IsInitialized = False Then Return
    Dim cs As CSBuilder
    For i = 0 To li.Size - 1
        Dim item As String = li.Get(i)
        Dim x As Int = item.ToLowerCase.IndexOf(full)
        If x = -1 Then
            Continue
        End If
        cs.Initialize.Append(item.SubString2(0, x)).Color(highlightColor).Append(item.SubString2(x, x + full.Length)).Pop
        cs.Append(item.SubString(x + full.Length))
        'lv.AddSingleLine(cs)
        Dim Row(2) As String
        Row(0) = i
        Row(1) = cs
        lv.AddSingleLine2(cs,Row)
    Next
End Sub

I understand the in the Sub AddItemsToList the index position keeps changing based on the characters typed by the user (the list in the ListView shrinks). So the position/index will not match with the original list
 
Last edited:
Upvote 0
Top