Android Question ContentResolver and phone Mobile Number filter

Daniel-White

Active Member
Licensed User
Longtime User
Thanks Erel, I am doing this approach, however it is so slowly, and put a black screen because take too much time, it is working fine show the correct values, but take 12 sec. and I guess put the black screen because the APP is too busy, the next code, show only the contacts using a mobile phone number and sorted by displayname. work fine, but soooooooooo slowly.

B4X:
   Dim allContacts As List = cu.FindAllContacts(True)
           allContacts.SortType("DisplayName", True)
          For Each c As cuContact In allContacts
            For Each phone As cuPhone In cu.GetPhones(c.Id)
                  'Log("cu.GetPhones(c.Id)= " &  cu.GetPhones(c.Id))
                If phone.PhoneType="mobile" Then         ' Filter  My interest is only the number with mobile attribute.
                 If temp <> c.Id Then                          ' Filter any repeated number.
                
                '    Log(c.DisplayName)        
                    clv3.add(CreateListItem(c.DisplayName,clv3.AsView.Width, 50dip), 50dip, c)
                    temp = c.Id
                    'Log("temp ="&temp)
                 End If    
                End If
            Next
        Next

I noticed the problem is in this part of For and Next. I had been checking the contactUtils class, I am not sure, perhaps in contactUtils put in list array and consume time, because put all the PhoneType in the list and later with my code filter only mobile from that list.
B4X:
  Dim allContacts As List = cu.FindAllContacts(True)
           allContacts.SortType("DisplayName", True)
          For Each c As cuContact In allContacts
            For Each phone As cuPhone In cu.GetPhones(c.Id)          '<----------  this is taking too much time  , this  For  next
            
                     'Nothing here on purpose



            Next
        Next


How can we improve the speed? Am I doing a bad approach? or minimal avoid the black screen, the ProgressDialogShow("bla bla bla") does not show neither.

Thanks you Erel and the community of B4A..
 
Upvote 0

Daniel-White

Active Member
Licensed User
Longtime User
Hi Erel Thanks for the hints, and put me to think a little bit :D , I share my code to help other :)

This code do the same, but only take 3 sec instead of 12 sec. :eek:

B4X:
Dim allContacts As List = cu.FindContactsWithONLYMOBILE
           allContacts.SortType("DisplayName", True)   
            For Each c As cuContact In allContacts
                  If temp <> c.DisplayName Then     'Not repeat the same contact only put one, with that is enough.                        
                    clv3.add(CreateListItem(c.DisplayName,clv3.AsView.Width, 50dip), 50dip, c)
                    temp = c.DisplayName
                  End If       
        Next
End Sub


And inside of ContactUtils class, that took me a little bit to figure out and discover how it work.

B4X:
'Returns all contacts with only mobile  phone number.
Public Sub FindContactsWithONLYMOBILE As List
    Return FindContactsIdFromData("vnd.android.cursor.item/phone_v2", "data2", 2, "=", True, False)
End Sub
 
Upvote 0
Top