Solution: Create Contact

Discussion in 'Old Libraries Archive' started by optimist, Jun 30, 2011.

  1. optimist

    optimist Member Licensed User

    Hello,
    the b4a phone library contains read only access to the contacts.
    but how about writable access? people often asked, but statement here was that it is not possible.

    But after a little bit research in this forum i mentioned that b4a has a very good support for external libraries. It seems, that almost all lower functions are loaded from an external library.

    So i wrote an library (the first in my live ;) ). Using it is is really easy:

    Code:
    Sub Button1_Click

      
    'Add libs phone and my miscUtil
      Dim mu As miscUtil, c As Contact
      mu.Initialize
      
    Dim name As String, phoneNr As String, phoneType As Int, mail As String, mailType As Int
      
    'define contact data
      name = "Mike Tester"
      phoneNr = 
    "004912345678"
      phoneType = c.PHONE_HOME
      mail = 
    "mike@tester.com"
      mailType = c.EMAIL_HOME
      
    'create contact
      mu.createContactEntry(name, phoneNr, phoneType, mail, mailType) 
      
    'thats all folks :-) 
       
      
    'check success, new contact should be the last entry in the list l
      Dim cs As Contacts
      
    Dim l As List
      l = cs.GetAll
      
    For i = 0 To l.Size - 1
       c = l.Get(i)
       
    Log(c)
      
    Next

    End Sub
    Sourcecode and binary of the lib are attached in miscUtil.zip
    Copy miscUtil.jar and miscUtil.jar to your libraries folder.

    b4a testproject is attached in libtest.zip

    Enjoy
    Michael
     

    Attached Files:

    Last edited: Jun 30, 2011
    jnjft likes this.
  2. optimist

    optimist Member Licensed User

    Version 1.01 of the lib is available.
    Add Contacts with multiple email-adresse and/or multiple phone-numbers

    Code:
    'Add libs phone and my miscUtil
      Dim mu As miscUtil, c As Contact
      mu.Initialize
      
    Dim name As String
      
    'define contact data
      name = "Kuno Maier"
      
    Dim phones As Map, mails As Map
      phones.Initialize
      phones.Put(c.PHONE_HOME, 
    "1234567890")
      phones.Put(c.PHONE_WORK, 
    "0987654321")
      mails.Initialize
      mails.Put(c.EMAIL_HOME, 
    "mike@tester.com")
      mails.Put(c.EMAIL_WORK, 
    "mike@testeroffice.com")

      
    'create contact
      mu.createContactEntry2(name, phones, mails)
    Suggestions welcome.
     
  3. gobblegob

    gobblegob Member Licensed User

    Thanks this is exactly what I need.

    One suggestion the name of the library implies that it does many different functions, maybe call it something more descriptive.


    cheers Waz
     
  4. mistermentality

    mistermentality Active Member Licensed User

    The zip file doesn't seem to contain a new version of the library only 1.0 , so your new example does not work :(

    Dave
     
  5. optimist

    optimist Member Licensed User

    Oh sorry for that, v1.04 is attached now.

    But it has even improved. You can read and write the photo of the Contact.

    Enjoy
     

    Attached Files:

  6. optimist

    optimist Member Licensed User

    This is exactly what it does for me. It provides all needed different functions for my app, not available directly in b4a. But only contact-management is interesting for common.
     
    Last edited: Jul 21, 2011
  7. mistermentality

    mistermentality Active Member Licensed User

    Brilliant, will be trying this later. Very much appreciated, am trying to use this as part of a contact widget so will be great having the new abilities. Thanks :)

    Dave
     
  8. mistermentality

    mistermentality Active Member Licensed User

    Quick question as a bit curious.

    I use this code:

    Code:
    phones.Initialize
      phones.Put(c.PHONE_HOME, number_entry_home.Text)
      phones.Put(c.PHONE_WORK, number_entry_work.Text)
      mails.Initialize
      mails.Put(c.EMAIL_HOME, email_entry.Text)
      mails.Put(c.EMAIL_WORK, email_entry.Text) 
    ' save same email for both entries
      ' create the new contact
      name = name_entry.Text

      mu.createContactEntry2(name, 
    Null, phones, mails, """"""NullNull)
    where I pass Null for the values of the photo, and the home and work addresses , but when the compiled app runs I am told that the map must first be initialised.

    As the only maps used are initialised in the above code does this mean instead of passing Null as a value is not allowed.

    I tried creating maps but still had Null for the picture value and instead then get error about "no such element". Is there a way to pass Null for the aspects of the contact we do not have such as the photo or home address as it looks like if not all details are filled in the contact can't be saved?

    Thanks.

    Dave
     
  9. optimist

    optimist Member Licensed User


    Thank you for the hint, i will permit Null instead Maps in the next version.

    For now please use initialized Maps (they accept empty strings if you dont know adresses)
    Your Code could be like:

    Code:
    Dim adressehome As  Map
    adressehome.Initialize
    adressehome.Put(
    0"")  'town
    adressehome.Put(1"")  'zip
    adressehome.Put(2"")  'street
    adressehome.Put(3"")  'country
    Dim adressework As  Map
    adressework.Initialize
    adressework.Put(
    0"")  'town
    adressework.Put(1"")  'zip
    adressework.Put(2"")  'street
    adressework.Put(3"")  'country

      mu.createContactEntry2(name, 
    Null, phones, mails, """""", adressehome, adressework)
     
  10. mistermentality

    mistermentality Active Member Licensed User

    Thank you for that, I have amended the code accordingly and it works great :)

    Dave
     
  11. TheDevMan

    TheDevMan Member Licensed User

    Hi optimist,

    Here is something for your lib.
    Delete all contacts.

    Code:
    public Boolean DeleteAllConcacts(){
           
    ContentResolver cr = this.m_ba.context.getContentResolver();
            
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                    
    nullnullnullnull);
            
    while (cur.moveToNext()) {
                
    try{
                    
    String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                    
    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
                    cr.delete(
    urinullnull);
                
    }
                catch(Exception e)
                {
                   return false;
                }
            }

           
          return true;
           
        }
     
  12. NJDude

    NJDude Expert Licensed User

    I wonder if there is going to be an option to create a GROUP in contacts.

    Looking good so far.
     
  13. optimist

    optimist Member Licensed User

    Get the Version 1.06 or higher and use createGroupEntry("MyNewGroup")

    Example:
    Code:
    Sub CreateGropTest

      
    Dim MyGroups As Map

      
    Dim j As Int
      
    Dim mu As miscUtil
      mu.Initialize

      
    'lets explore the current available groups
      MyGroups = mu.GetGroupMap
      
    For j=0 To MyGroups.Size - 1
        
    Log("Group " & MyGroups.GetKeyAt(j) & " - " & MyGroups.GetValueAt(j) )
      
    Next

      
    'create the new group
      mu.createGroupEntry("MyNewGroup")
      
      
    'lets explore the current available groups again
      'map shold contain the new group now
      MyGroups = mu.GetGroupMap
      
    For j=0 To MyGroups.Size - 1
        
    Log("Group " & MyGroups.GetKeyAt(j) & " - " & MyGroups.GetValueAt(j) )
      
    Next

    End Sub
     
    Last edited: Jul 26, 2011
  14. optimist

    optimist Member Licensed User

    :sign0098:

    I would never, never call this function in real live, because i collected my contacts over a log time. :)

    Attached new version v1.06 contains your contribution. Thank you.
     

    Attached Files:

  15. NJDude

    NJDude Expert Licensed User

    FANTASTIC!!!, simple yet useful, thanks, so far is working fine.

    Question, this lib is for the address book only right?, you cannot add entries to the calendar correct?


    In the future, will this allow to delete a single or selected entries?, deleting the whole contacts list will be rare, but single entries might become useful.
     
    Last edited: Jul 26, 2011
  16. NJDude

    NJDude Expert Licensed User

    One of the tooltips is in german.

    See attached screen shot.
     

    Attached Files:

  17. TheDevMan

    TheDevMan Member Licensed User


    Yeah, i know. But i needed it for one of my backup apps. Its working cool!
    Its more strange when you do a backup and restore it over the others, that way you get double entries. :sign0142:
     
  18. JOTHA

    JOTHA Well-Known Member Licensed User

    Hello optimist,

    GOOD WORK! :sign0188: RESPECT!

    Will you add more features like ...

    • Birthday
    • First Name
    • Middle Name
    • Last Name
    • Title
    • Suffix
    • Initials
    • Web Page
    • Gender
    • Anniversary
    • Location
    • Language
    • Notes
    • Spouse
    • Children
    • Categories
    • Company
    • JobTitle
    • Hobby
    • ... etc. ... (if you wish I could send you a list)

    ... this would be GREAT !!!
     
  19. Ronny

    Ronny Member Licensed User

    Hi

    I need to add several (hundred) numbers to one contact. Is that possible with miscUtil?

    /Ronny
     
  20. optimist

    optimist Member Licensed User

    No, and currently not planned in the free lib. But i am ready to prioritize special features for sponsors.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice