Using AutoCompleteEditText to search/filter contacts?

susu

Well-Known Member
Licensed User
Longtime User
I'm trying to use AutoCompleteEditText to search/filter contacts. My code as below:

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim ContactList As List
   Dim NameSearch As AutoCompleteEditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("main")
   ContactList.Initialize
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub SearchContact(s As String)
   Dim Contacts2 As Contacts2
   Dim listOfContacts As List
   listOfContacts = Contacts2.FindByName(s, False,  True, False) 'No need Note
   For i = 0 To listOfContacts.Size - 1
       Dim Contact As Contact
       Contact = listOfContacts.Get(i)
      
      Dim phones As Map
       phones = Contact.GetPhones   ' Get all the phone numbers
      If phones.Size > 0 Then 
         For j = 0 To phones.Size - 1
         ContactList.Add(Contact.DisplayName & " (" & phones.GetKeyAt(j) & ")")  ' To display like: Santa Clause (0011223344)
         Next
      End If
   Next  

   NameSearch.SetItems(ContactList)
End Sub

Sub Name_ItemClick (Value As String)
   ToastMessageShow(Value, False)   ' Error?
End Sub

Sub Name_TextChanged (Old As String, New As String)
   ContactList.Clear    'Clear list before search
   SearchContact(New)
End Sub

1. It worked ok on emulator with 4-5 example contacts but I got error when click on the drop down menu. I don't know why?
2. When test on my real device with more than 200 contacts, it's very slow to type characters and it even doesn't show drop down menu. Could you please make some suggestions to improve it?

Thank you so much
 

susu

Well-Known Member
Licensed User
Longtime User
Thank you Erel. I edited my code like below:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("main")
   ContactList.Initialize
   NameSearch.Initialize("NameSearch")
   Dim Contacts2 As Contacts2
   Dim listOfContacts As List
   listOfContacts = Contacts2.GetAll(True, False)  'No need Notes
   For i = 0 To listOfContacts.Size - 1
       Dim Contact As Contact
       Contact = listOfContacts.Get(i)
      
      Dim phones As Map
       phones = Contact.GetPhones   ' Get all the phone numbers
      If phones.Size > 0 Then 
         For j = 0 To phones.Size - 1
         ContactList.Add(Contact.DisplayName & " (" & phones.GetKeyAt(j) & ")")  ' To display like: Santa Clause (0011223344)
         Log(Contact.DisplayName & " (" & phones.GetKeyAt(j) & ")")  ' Yes, I see contacts in Logs
         Next
      End If
   Next  
   
   ToastMessageShow(ContactList.Size, False)  ' Yes, it shows the number of items
   NameSearch.SetItems(ContactList)
   
End Sub

It print all names with phone numbers in Logs but not in AutoCompleteEditText when I type name. What's wrong here?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It would be useful if you could zip and post the project, it'd be easier to find the problem.

Did you create NameSearch in the designer? If so you don't need to initialize it again. This may be the (only) problem but I can't test it to tell you definitely.
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Stevel05, you remind me that I changed AutoCompleteEditText's name in code but forgot to change in Designer Thank you so much.
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
On real device with more than 200 contacts it takes about 3 sec to load so I add:

ProgressDialogShow2("Loading contacts, please wait ", False)
'Code to load contact...
ProgressDialogHide

But the progress dialog didn't show up?
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Because contact loading takes time so I just want to load it when app first run:

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
InitContact
End If
End Sub

When I run my app first time, it load all the contacts and AutoCompleteEditText works ok. But when I close my app (using back button or Activity.Finish) and run it again AutoCompleteEditText does not work. I try to Dim ContactList in Sub Process_Globals but still no luck.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You will still need to load the items into the AutoCompleteEditText, even if you have defined contactlist as a process global. Presumably that is done in initcontacts as well as reading them from the phone.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…