Android Question GetContactsByQuery

leitor79

Active Member
Licensed User
Longtime User
Hi,

Can anyone provide a link to an example or documentation of this Contacts2 function? I'm trying to use it (I want all contacts with phone number) but I can´t find how to write the sql sentence...

B4X:
"Select * From Contacts where PhoneNumber <> '' and PhoneNumber is not null"

PhoneNumber does not exists, and I don't know if "Contacts" is the right name for the table.

Regards!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Hi Erel, thank you for your answer.

I've managed to get the contact list either by contacts2 and ContactsUtils; I will use contactsutils as you recommend.

But, looking at the code I've made, I feel it could be improved. I'm not familiar with this kind of "sql" but I see I'm making 2 queries, the second one with the ID I get from the first... can't an only one query with a join be done?

I paste my code:

B4X:
Private Sub FindContactsWithNumber() As List
   
    Dim selection As String = "mimetype = ? AND length(data1) > 0"
    Dim Mime As String="vnd.android.cursor.item/phone_v2"
    Dim sf As StringFunctions   
    Dim crsr As Cursor = cr.Query(dataUri, Array As String("contact_id", "display_name"), selection,  Array As String(Mime), "")
    Dim m As Map
    Dim res As List
   
    sf.Initialize   
    res.Initialize
   
   
    m.Initialize
    For i = 0 To crsr.RowCount - 1
        crsr.Position = i
        Dim cu As cuContact
        cu.Initialize
        cu.Id = crsr.GetLong("contact_id")
        cu.DisplayName = crsr.GetString("display_name")

        cu.Numbers=sf.ListToString(GetData(cu.Id),False,False)

        Log(cu)
       
        If m.ContainsKey(cu.Id) Then Continue
        m.Put(cu.Id, Null)
        res.Add(cu)
       
    Next
    crsr.Close
    Return res
End Sub



Private Sub GetData(Id As Long) As List
   
    Dim Mime As String="vnd.android.cursor.item/phone_v2"
    Dim crsr As Cursor = cr.Query(dataUri, Array As String("data1", "data2"), "mimetype = ? AND contact_id = ?", _
    Array As String(Mime, Id), "")
    Dim res As List
   
    res.Initialize
    For i = 0 To crsr.RowCount - 1
        crsr.Position = i
        res.Add(crsr.GetString2(0))
    Next
    crsr.Close
    Return res
   
End Sub


Thank you very much!
 
Upvote 0
Top