Android Question [Solved] How to get contact list with mobile phone?

asales

Expert
Licensed User
Longtime User
I have this sub (FindContactsWithMobilePhone) in ContactUtils:
https://www.b4x.com/android/forum/t...ist-with-only-mobile-phone.94736/#post-599282
but the FindContactsIdFromData returns only DisplayName and Id in list.

How I can get the mobile number too in the list?

My code:
B4X:
Dim ctu As ContactsUtils
Dim allContacts As List = ctu.FindContactsWithMobilePhone
Log(allContacts)
'(ArrayList) [[DisplayName=Customer, Id=14, IsInitialized=True
'], [DisplayName=Hans Bikes, Id=290, IsInitialized=True
'...
 

Peter Simpson

Expert
Licensed User
Longtime User
I take it that you are just running the code with no changes from THIS LINK. Even using the code provided in the link with no changes whatsoever I'm reading for example my sisters home number, mobile number, spare mobile number and work mobile number.

I've just ran the example code with no issues whatsoever, all numbers read as expected...
 
Upvote 0

asales

Expert
Licensed User
Longtime User
I take it that you are just running the code with no changes from THIS LINK.
Yes.
Even using the code provided in the link with no changes whatsoever I'm reading for example my sisters home number, mobile number, spare mobile number and work mobile number.
I've just ran the example code with no issues whatsoever, all numbers read as expected...
I can get the phone numbers with the sub "GetPhones" to each contact.

I try to use the "FindContactsWithMobilePhone" sub to return the a list of all contacts (like sub "FindAllContacts") with DisplayName, ID and MobilePhone, but it is not happening. The "FindContactsWithMobilePhone" sub returns only DisplayName and Id in list, not the mobile phone number.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The "FindContactsWithMobilePhone" sub returns only DisplayName and Id in list, not the mobile phone number.
That's true. It doesn't extract the phone numbers.

You can use this sub to get all stored phone numbers with a single query:
B4X:
'add to ContactUtils
Public Sub GetAllPhones As Map
   Dim crsr As ResultSet = cr.Query(dataUri, Array As String("data1", "data2", "contact_id"), "mimetype = ?", _
       Array As String("vnd.android.cursor.item/phone_v2"), "")
   Dim res As Map
   res.Initialize
   Do While crsr.NextRow
       Dim id As Long = crsr.GetLong("contact_id")
       Dim phones As List
       If res.ContainsKey(id) = False Then
           phones.Initialize
           res.Put(id, phones)
       Else
           phones = res.Get(id)
       End If
       Dim p As cuPhone
       p.Initialize
       p.Number = crsr.GetString2(0)
       p.PhoneType = phoneTypes.Get(crsr.GetString2(1))
       phones.Add(p)
   Loop
   crsr.Close
   Return res
End Sub

Usage example:
B4X:
Dim phones As Map = cu.GetAllPhones
For Each id As Long In phones.Keys
   Dim idphones As List = phones.Get(id)
   Log($"${id}: ${idphones}"$)
Next
 
Upvote 0
Top