Android Question loading contacts is slow

Mohamed Ghoname

Member
Licensed User
Longtime User
Dear Earl,
Realy good work. Thank you for this awesome tool.

Now I'm trying to make a contacts manager tool but when I'm using the below code its take about 9 seconds to just fill a in memory list loading name and id (not list view or no any control) ok! but the real question is how android's contact manager (the built in one) loads just in time I open it name and ID and image in addition to loading it in the control hhhhhh !!!! please help as i went crazy to make it faster but no way
here is my code (I bring it from your fourm :) )


Public Sub testGetAll2() As List
Dim resolver As ContentResolver
resolver.Initialize("res")
Dim Uri1 As Uri

Uri1.parse("content://com.android.contacts/data")
Dim c As cuContact
c.Initialize
Dim rslt As List
rslt.Initialize
Dim selection As String = "mimetype = ? AND data1 <> ? "
Dim crsr As Cursor = resolver.query(Uri1,Array As String( "contact_id","display_name","data1"),selection, Array As String("vnd.android.cursor.item/name", "null"), "")
For x= 0 To crsr.RowCount-1
crsr.Position=x
c.Id=crsr.GetString2(0)
c.DisplayName=crsr.GetString2(1)
rslt.Add(c)
Next
Return rslt
End Sub
 

bsnqt

Active Member
Licensed User
Longtime User
Follow this code and adapt to your need, it will load all 700 - 1,000 contacts less than a second to the list.
B4X:
Sub GetAllContacts As List
   
    Dim myList As List
    myList.Initialize
    Private phonesUri As Uri
    phonesUri.Parse("content://com.android.contacts/data/phones")
    Private cr As ContentResolver
    cr.Initialize("mycontentresolver")
   
    Private phones As Cursor = cr.Query(phonesUri, Array As String("display_name", "data1"),"", Null, "display_name")
    For p = 0 To phones.RowCount - 1
        phones.Position = p
               myList.Add(phones.GetString("display_name") & CRLF & phones.GetString("data1"))
     Next
    phones.Close
    Return myList
   
End Sub
 
Upvote 0
Top