Android Question Query Contact

MarcoRome

Expert
Licensed User
Longtime User
Hi all.
i have in my Card Sim 3 contact ( name, number, email ).
Now.. i utilize this code:

B4X:
......
ListView2.AddTwoLinesAndBitmap2( crsr.GetString("name"), crsr.GetString("number") & " - Email:" & crsr.GetString("emails"),LoadBitmap(File.DirAssets,"simcard.png"),crsr.GetLong("_id"))
 Next
   crsr.Close
End Sub
 
'Copia il Share, Copia Numero, invia SMS, Chiama
Sub ListView2_ItemClick (Position As Int, Value As Object)
   Dim simUri As Uri
   Dim verifica_nome As String
   MsgBox( Value, "Check") '<------ Here _id is correct, 0, 1, 2 etc....
   simUri.Parse("content://icc/adn") '
   Dim crsr As Cursor = cr.Query(simUri, Array As String("_id", "emails", "number", "name"), "_id = ?", Array As String(Value), "")
 
'HERE i WAIT 1 VALUE BUT I HAVE ALL VALUE ( 3 ROW )    
   For i = 0 To crsr.RowCount - 1
      crsr.Position = i
      verifica_nome = crsr.GetString("name")
   Next
   crsr.Close   
   Msgbox(verifica_nome,"Messaggio")

The Query is:
B4X:
  Dim crsr As Cursor = cr.Query(simUri, Array As String("_id", "emails", "number", "name"), "_id = ?", Array As String(Value), "")

But i have all value present in SIM Card, where i wrong ???

Thank you all
Marco
 

MarcoRome

Expert
Licensed User
Longtime User
The some Query work without problem if i dont work about SIM.
If i change this code :
B4X:
simUri.Parse("content://icc/adn") and put --- > simUri.Parse("content://com.android.contacts/contacts")


B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim cr As ContentResolver
End Sub
 
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
 
    Private ListView1 As ListView
End Sub
 
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("1")
    ReadSimContacts
 
End Sub
 
 
Sub ReadSimContacts
   Dim simUri As Uri
   simUri.Parse("content://com.android.contacts/contacts")      '("content://icc/adn")
   Dim crsr As Cursor = cr.Query(simUri, Null, "", Null, "")
   For i = 0 To crsr.RowCount - 1
      crsr.Position = i
      ListView1.AddSingleLine2(crsr.GetString("display_name"), crsr.GetLong("_id"))
   Next
   crsr.Close
End Sub
 
Sub Activity_Resume
 
End Sub
 
Sub Activity_Pause (UserClosed As Boolean)
 
End Sub
 
 
 
Sub ListView1_ItemClick (Position As Int, Value As Object)
    Dim simUri As Uri
   Dim verifica_nome As String
   Dim valore As Long = Value
   simUri.Parse("content://com.android.contacts/contacts")'("content://icc/adn")
   Dim crsr As Cursor = cr.Query(simUri,Array As String("_id", "display_name"), "_id = ?", Array As String(Valore), "")
 
   
   For i = 0 To crsr.RowCount - 1
      crsr.Position = i
      verifica_nome = crsr.GetString("display_name")
   Next
   crsr.Close  
   Msgbox(verifica_nome,"Messaggio")
End Sub

The result is right.

Bug or what ?
Any idea ?
Thank you
Marco
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Anyway RESOLVE in this mode. In ListView i load this:

B4X:
simUri.Parse("content://icc/adn") '
   Dim crsr As Cursor = cr.Query(simUri, Array As String("_id", "emails", "number", "name"), "_id = ?", Array As String(Value))

After the query work about this:
B4X:
simUri.Parse("content://com.android.contacts/contacts")

This is code:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("1")
    ReadSimContacts
 
End Sub
 
 
Sub ReadSimContacts
   Dim simUri As Uri
   simUri.Parse("content://icc/adn")
   Dim crsr As Cursor = cr.Query(simUri, Null, "", Null, "")
   For i = 0 To crsr.RowCount - 1
      crsr.Position = i
      ListView1.AddSingleLine2(crsr.GetString("name"), crsr.GetString("name"))
   Next
   crsr.Close
End Sub
 
Sub Activity_Resume
 
End Sub
 
Sub Activity_Pause (UserClosed As Boolean)
 
End Sub
 
 
 
Sub ListView1_ItemClick (Position As Int, Value As Object)
    Dim simUri As Uri
   Dim verifica_nome As String
   Dim verifica_telefono As String
   Dim verifica_email As String
  
   simUri.Parse("content://com.android.contacts/contacts")'("content://icc/adn")
   Dim crsr As Cursor = cr.Query(simUri,Array As String("_id", "display_name"), "display_name = ?", Array As String(Value), "")
 
    
   For i = 0 To crsr.RowCount - 1
      crsr.Position = i
      'Prelevo Nome
      verifica_nome = crsr.GetString("display_name")
     'Prelevo id per leggere Telefono e Email
      Dim id As Long = crsr.GetLong("_id")
    
   Next
   crsr.Close
  
     
      'print phones
      Dim phonesUri As Uri
      phonesUri.Parse("content://com.android.contacts/data/phones")
      Dim phones As Cursor = cr.Query(phonesUri, Array As String("data1", "data2"), _
         "contact_id = ?", Array As String(id), "")
      For p = 0 To phones.RowCount - 1
         phones.Position = p
         verifica_telefono = "Phone: " & phones.GetString("data1")
      Next
      phones.Close
     
      'print emails
      Dim emailsUri As Uri
      emailsUri.Parse("content://com.android.contacts/data/emails")
      Dim emails As Cursor = cr.Query(emailsUri, Array As String("data1"), _
         "contact_id = ?", Array As String(id), "")
      For p = 0 To emails.RowCount - 1
         emails.Position = p
         verifica_email = "Email: " & emails.GetString("data1")
      Next
      emails.Close
 
       Msgbox(verifica_nome & CRLF & verifica_telefono & CRLF & verifica_email ,"Messaggio")
End Sub

Thank you anyway.
Marco
 
Upvote 0
Top