Sort contacts

Christian

Member
Licensed User
Longtime User
Hi Erel,
the code in the attached zip file displays the name and the phone number of the
contacts which are stored on the phone. Now I want to sort this list by names.
Do you have an idea how to achieve it.
Best regards
Christian
 

Attachments

  • Contacts.zip
    4.8 KB · Views: 539

Erel

B4X founder
Staff member
Licensed User
Longtime User
Small tip, if your code doesn't depend on any other files then it is better to just post the code.

The solution I've implemented here is to add all the display names to a list and to have a map that maps between the names and the contacts.
The "display names" list can be sorted because it is a list of strings.
B4X:
Sub Globals
    Dim id As Int
    Dim Contacts1 As Contacts
    Dim lvPersons As ListView
    Dim myContact As Contact
    Dim listOfContacts As List
End Sub

Sub Activity_Create(FirstTime As Boolean)
    lvPersons.Initialize("")
    Activity.AddView(lvPersons,0%x,0%y,100%x,100%y) 'don't confuse %x and %y
    lvPersons.Clear
    
    listOfContacts = Contacts1.GetAll
    Dim map1 As Map
    map1.Initialize
    Dim list1 As List
    list1.Initialize
    For i = 0 To listOfContacts.Size - 1
        myContact = listOfContacts.Get(i)
        list1.Add(myContact.DisplayName)
        map1.Put(myContact.DisplayName, myContact)
    Next
    list1.Sort(True)
    For i = 0 To list1.Size - 1
        myContact = map1.Get(list1.Get(i))
        lvPersons.AddTwoLines2(myContact.DisplayName,myContact.PhoneNumber,myContact.id)
    Next
End Sub

Note that if there are two contacts with the same display name then the same contact will be displayed twice.
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Erel, I tried your code but on emulator and my phone it doesn't show any phone number! I faced this problem for long time and I still can't solve it. Please support.

attachment.php
 

Attachments

  • contact.jpg
    contact.jpg
    8.8 KB · Views: 1,067
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Ok, it show phone numbers but how to display mobile phone only? For example: Batman 555-555-55.

attachment.php
 

Attachments

  • contact2.jpg
    contact2.jpg
    13.4 KB · Views: 1,077
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Thank you Erel but your code is not correct some places. I edited it like this:

Sub Activity_Create(FirstTime As Boolean)

lvPersons.Initialize("")
Activity.AddView(lvPersons,0%x,0%x,100%x,100%x)
lvPersons.Clear

listOfContacts = Contacts1.GetAll

For i = 0 To listOfContacts.Size - 1
myContact = listOfContacts.Get(i)
Dim mob As Map
Dim mobile As String
mob = myContact.GetPhones
For x = 0 To mob.Size -1
If mob.GetValueAt(x) = myContact.PHONE_MOBILE Then
mobile = mob.GetKeyAt(x)
lvPersons.AddTwoLines2(myContact.DisplayName,mobile,myContact.id)
Exit
End If
Next

Next

End Sub

The BIG problem is it works like a champ on emulator but on my real phone nothing appears! I reported this problem before and I think there's something wrong with the contact library. Can you check again?

Ps: My phone is running Android 2.1.
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Erel, do you have Samsung Galaxy Tab? Can you check the code above?
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Your Galaxy Tab is running Android 2.2?
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
As I said before, Samsung phone with Android 2.1 got problem with these contact thing and I still don't know how to fix it :(
 
Upvote 0

rcaridade

Member
Licensed User
Longtime User
I implement this way to read the contacts it very slow but it work, it has two ways to display the list, if there is something in the textbox it uses it as a filter otherwise brings all contacts.

the code:

Sub btnContactos_Click
Dim contactos As Contacts
Dim contacto As Contact
Dim listaContactos As List
Dim telefones As Map
Dim telemovel As String
Dim listaNomes As List
Dim listaTelefones As List
Dim listaGeral As Map
Dim selecionado As Int
Dim inicio As String

ProgressDialogShow("A carregar a lista de contactos...")

listaNomes.Initialize
listaTelefones.Initialize
listaGeral.Initialize

DoEvents

If txtNumero.Text.Length = 0 Then
listaContactos = contactos.GetAll

Else
listaContactos = contactos.FindByName(txtNumero.Text.Trim , False)

End If


For i = 0 To listaContactos.Size - 1

DoEvents

Contacto = listaContactos.Get(i)

If contacto.DisplayName.IndexOf("@") < 0 Then
telefones = Contacto.GetPhones

For j = 0 To telefones.Size -1

If telefones.GetValueAt(j) = Contacto.PHONE_MOBILE Then
listaNomes.Add(contacto.DisplayName)
listaGeral.Put(contacto.DisplayName,contacto)
Exit
End If

Next
End If

Next

listaNomes.SortCaseInsensitive(True)

For i = 0 To listaNomes.Size - 1

DoEvents

Contacto = listaGeral.Get(listaNomes.Get(i))
telefones = Contacto.GetPhones

For j = 0 To telefones.Size -1

If telefones.GetValueAt(j) = Contacto.PHONE_MOBILE Then
telemovel = telefones.GetKeyAt(j)
listaTelefones.Add(telemovel)
Exit
End If

Next
Next

ProgressDialogHide

selecionado = InputList(listaNomes, "Contactos", -1 )
If selecionado <> DialogResponse.CANCEL Then
telemovel = listaTelefones.Get(selecionado)
txtNumero.Text = telemovel.Replace("+351","").Trim
Else
txtNumero.Text = ""
End If
End Sub
 
Upvote 0

elbelli2013

Member
Licensed User
Longtime User
Hi, I need to sort my list, but when I try to do throw this error:

Dim listOfContacts As List


If FirstTime Then

ProgressDialogShow("Building contacts index...")

listOfContacts.Initialize
listOfContacts.Clear

listOfContacts = Contacts2.GetAll(True,False)
listOfContacts.SortType("DisplayName", True )

End If




** Activity (main) Create, isFirst = true **


Error occurred on line: 117 (main)
java.lang.NoSuchFieldException: DisplayName


at java.lang.ClassCache.findFieldByName(ClassCache.java:446)
at java.lang.Class.getDeclaredField(Class.java:666)
at anywheresoftware.b4a.objects.collections.List.sortList(List.java:193)
at anywheresoftware.b4a.objects.collections.List.SortType(List.java:181)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:680)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:308)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at b4a.example.main.afterFirstLayout(main.java:98)
at b4a.example.main.access$100(main.java:16)
at b4a.example.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3744)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)


Anybody can help me??

thanks in advance
 
Last edited:
Upvote 0
Top