Android Question How can I get phone mumber from ContactsUtils

Discussion in 'Android Questions' started by Beja, Mar 2, 2015.

  1. Beja

    Beja Expert Licensed User

    Hello,
    Something weird.. I used the code below to get all phone contacts in a list.. instead I get the contacts of my Skype (not the phone).
    Any help appreciated.

    Code:
    Sub lstContacts_ItemClick (Position As Int, Value As Object)
        
    Dim c As cuContact = Value
        
    Dim bmp As Bitmap = cu.GetPhoto(c.Id)
        
    If bmp.IsInitialized Then ImageView1.SetBackgroundImage(bmp) Else ImageView1.SetBackgroundImage(Null)
        
    Dim sb As StringBuilder
        sb.Initialize
        
    'sb.Append(c.DisplayName).Append(CRLF).Append("Note: ").Append(cu.GetNote(c.Id)).Append(CRLF)
        'sb.Append("Starred: ").Append(cu.GetStarred(c.Id)).Append(CRLF)
        
        
    For Each phone As cuPhone In cu.GetPhones(c.Id)
            sb.Append(
    phone.Number & ", " & phone.PhoneType).Append(CRLF)
        
    Next
        
    For Each email As cuEmail In cu.GetEmails(c.Id)
            sb.Append(
    email.email).Append(", ").Append(email.EmailType).Append(CRLF)
        
    Next
        EditText1.Text = sb.ToString
    End Sub

    Sub lstContacts_ItemLongClick (Position As Int, Value As Object)
        
    'toggle the starred state
        Dim c As cuContact = Value
        cu.SetStarred(c.Id, 
    Not(cu.GetStarred(c.Id)))
        
    'update the text
        lstContacts_ItemClick(Position, Value)
    End Su
     
  2. Beja

    Beja Expert Licensed User

    I uninstalled Skype with all its contacts.. now the example app doesn't show my phone contacts..
    Any help please.. this is a disaster for me.

    edit:
    I am using Alcatel onetouch - 7040N, and Android 4.4.2
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Where is the code that gets the contacts?
     
  4. Beja

    Beja Expert Licensed User

    Hi Erel,

    I assume it is this one:
    Code:
    For Each phone As cuPhone In cu.GetPhones(c.Id)
            sb.Append(
    phone.Number & ", " & phone.PhoneType).Append(CRLF)
        
    Next
    [code]
    (extracted from the code above)
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    This code gets all the listed phone numbers of the specific contact id. Where is the code that actually finds all the ids?

    You can upload the complete project (File - Export as zip).
     
  6. mark35at

    mark35at Well-Known Member Licensed User

    This any help to you?

    Code:
    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.

    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.
        Dim details, cName, cphone As String
        
    Dim listOfContacts As List
        
    Dim lvphone As ListView
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    Activity.LoadLayout("main")
        getcontacts
    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub

    Sub getcontacts
        
    Dim Contacts2 As Contacts2
        listOfContacts.Initialize
        listOfContacts = 
    Contacts2.GetAll(True,False)
        
    'Log(listOfContacts)
       
          
    'listOfContacts.Sort(True)
        'Log(listOfContacts)
           
        
    For i = 0 To listOfContacts.Size - 1
            
    Dim Contact As Contact
            
    Contact = listOfContacts.Get(i)
            
    'Log(Contact) 'will print the fields to the LogCat
     
            
    If Contact.PhoneNumber <>"" Then 
                
    'If there Is a Contact number pass it To the ListView so you can see it
                                    
                
    'lvphone.TwoLinesLayout.SecondLabel.textcolor=Colors.blue
                lvphone.SingleLineLayout.Label.TextSize = 20
                lvphone.SingleLineLayout.ItemHeight = 
    70
                lvphone.AddSingleLine(
    Contact.DisplayName &CRLF &Contact.PhoneNumber)
                
    'lvphone.AddTwoLines(Contact.DisplayName,Contact.PhoneNumber)
                lvphone.ScrollingBackgroundColor =Colors.DarkGray
             
    End If
        
    Next  

    End Sub

    Sub lvphone_ItemClick (Position As Int, Value As Object)  As String
        
         details = Value
         
    'Log("******************************************")
         'Log("selected " & details)  'the name in the logs As a check
       
        
    'Log("Break found at " & details.IndexOf(CRLF))
        'Log("******************************************")
        cName=details.SubString2(0,details.IndexOf(CRLF))
       
       
        
    Log("Name selected -" & cName & "-")
        cphone=details.SubString2(details.IndexOf(
    CRLF)+1,details.Length)
       
       
        
    Log("Phone number -" & cphone & "-")
        
    Activity.Title=cName & "-" & cphone
        
    Return details
       
    End Sub
    Is old code but should still work.
     
  7. Beja

    Beja Expert Licensed User

    Hi Erel
    it is the same as contactsutils sample
     

    Attached Files:

    Last edited: Mar 3, 2015
  8. mark35at

    mark35at Well-Known Member Licensed User

    Yes, this is true. What about using SQL? I have been using this for years with Skype installed, no problem.
     

    Attached Files:

  9. Erel

    Erel Administrator Staff Member Licensed User

    Try to change cu.FindAllContacts(True) to cu.FindAllContacts(False) which will remove the visible only filter.
     
  10. Beja

    Beja Expert Licensed User

    Yep!
    Thank you Erel.. Now it works like a charm. All the contact info is out.. (phone numbers, emails, etc)

    @mark,
    Thanks and I would try the SQL method, but I didn't know how to design the "main.bal" and it's missing.
     
  11. mark35at

    mark35at Well-Known Member Licensed User

    Sorry, the code in post #6 is not runable as it is only part of the app. My code in post #8 is also only partial. Here is the complete code for the SQL example if you wish.
     

    Attached Files:

    • App.zip
      File size:
      29.5 KB
      Views:
      91
  12. Beja

    Beja Expert Licensed User

    Hello Mark,
    Your project is perfect and very professional.. thanks for sharing..
    Notice: navigation information is important.. either by help file or use of menus, tags or buttons. Now (I think) it depends on
    common sense of experienced users. for example, when clicking on Setup, and the setup window comes up, then it would be
    very helpful to drop a line of text explaining what to do next.. for example:
    Click an item to show more options.. then when clicking an item and go there, another line saying, click on the name to send email, or on the
    number to send SMS..
    I mean something like this for all the steps will be very helpful for users.
    Thanks again for the great work and generous sharing.
     
    mark35at likes this.
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