Android Question How to list all contacts phone numbers with StartActivityForResult?

susu

Well-Known Member
Licensed User
Longtime User
Hi,

I'm using this code to pick phone numbers from Contacts.

B4X:
 Sub Activity_Click
   Dim i As Intent
   i.Initialize(i.ACTION_PICK, "content://com.android.contacts/contacts")
   StartActivityForResult(i)
End Sub

Sub ion_Event (MethodName As String, Args() As Object) As Object
   If Args(0) = -1 Then 'resultCode = RESULT_OK
     Dim i As Intent = args(1)
     Dim id As String = i.GetData.SubString(i.GetData.LastIndexOf("/") + 1)
     Dim cu As ContactsUtils
     cu.Initialize
     Log(cu.GetNameFromId(id))
   End If
   Return Null
End Sub

B4X:
Public Sub GetNameFromId (id As String) As String
   Dim crsr As Cursor = cr.Query(dataUri, Array As String("contact_id", "display_name"), "contact_id = ?", _
     Array As String(id), "")
   Dim name As String
   If crsr.RowCount = 0 Then
     Log("Contact not found: " & id)
   Else
     crsr.Position = 0
     name = crsr.GetString2(1)
   End If
   crsr.Close
   Return name
End Sub

It worked great if contact only have 1 phone number. I tried another app which can list all phone numbers, just like screen shot below. How can I archive it? Thank you.

contacts.jpg
 

susu

Well-Known Member
Licensed User
Longtime User
Why aren't you using ContactsUtils?
I just want to use simplest way to pick a number. If I used ContactsUtils, I need to create an Activity... then load all contacts into it... then create event when user pick a contact... So it's more complicated :D
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
I change the code:

B4X:
Sub ion_Event (MethodName As String, Args() As Object) As Object
   If Args(0) = -1 Then 'resultCode = RESULT_OK
     Dim i As Intent = Args(1)
     Dim id As String = i.GetData.SubString(i.GetData.LastIndexOf("/") + 1)
     Dim cu As ContactsUtils
     cu.Initialize
    cu.FindAllContacts(True)
    Dim ll As List
    ll = (cu.GetPhones(id))
   End If
          For x=0 To ll.Size-1
              Log(ll.Get(x))
          Next
   Return Null
End Sub

It show all phone numbers:
B4X:
[IsInitialized=true, Number=123 456, PhoneType=mobile
]
[IsInitialized=true, Number=99 999, PhoneType=work
[
 
Upvote 0
Top