Android Question Problem with cu.InsertContact(p1,p2)

Dogbonesix

Active Member
Licensed User
Longtime User
I can insert a Phone Contact using cu.InsertContact(p1,p2) where p1 is the Name and p2 is the phone number.

But, I am not to sure how to set the type when inserting a contact. I have looked at a lot of samples but nothing seems to work.

Dim p As cuPhone
p.PhoneType = "mobile"

Does not work.

I could be close, I could be way off base. Any help appreciated.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Replace InsertContact with this code:
B4X:
Public Sub InsertContact(Name As String, Phone As String, PhoneType As String) As cuContact
   Dim values As ContentValues
   values.Initialize
   values.PutNull("account_name")
   values.PutNull("account_type")
   Dim rawUri As Uri = cr.Insert(rawContactUri, values)
   Dim rawContactId As Long = rawUri.ParseId
   
   values.Initialize
   values.PutLong("raw_contact_id", rawContactId)
   values.PutString("mimetype", "vnd.android.cursor.item/phone_v2")
   values.PutString("data1", Phone)
   values.PutInteger("data2", GetKeyFromValue(phoneTypes, PhoneType, 7))
   cr.Insert(dataUri, values)
   
   values.Initialize
   values.PutLong("raw_contact_id", rawContactId)
   values.PutString("mimetype", "vnd.android.cursor.item/name")
   values.PutString("data1", Name)
   cr.Insert(dataUri, values)
   Dim cu As cuContact
   cu.Initialize
   Dim crsr As Cursor = cr.Query(dataUri, Array As String("contact_id", "display_name"), "raw_contact_id = ?", _
     Array As String(rawContactId), "")
   crsr.Position = 0
   cu.DisplayName = crsr.GetString("display_name")
   cu.Id = crsr.GetLong("contact_id")
   Return cu
End Sub

I haven't tested it. Please report whether it worked or not.
 
Upvote 0
Top