Contacts

bergapappa

Member
Licensed User
Longtime User
Hi all :sign0085:

Going crazy trying to addapt a code found on the forum to my needs.
Concerning Contacts, and I want to return the Contacts Mobile Number.
The original syntax returned the default phone number.
My code returns "2" for all contacts.

Can anyone give me an hint of what I'm doing wrong?
B4X:
Sub Button1_Click
    Dim Contacts2 As Contacts2
    Dim list1 As List
    list1 = Contacts2.GetAll(False,False)
    Dim listOfNames As List
    listOfNames.Initialize
   
    For i = 0 To list1.Size - 1
        Dim c As Contact
        c = list1.Get(i)
        If c.DisplayName.IndexOf("@") = -1 Then 
            listOfNames.Add(c.DisplayName)
        End If
    Next
    Dim res As Int
    res = InputList(listOfNames, "Choose contact", -1)
    If res <> DialogResponse.CANCEL Then
        Dim name As String
        name = listOfNames.Get(Res)
        Dim c As Contact

        For i = 0 To list1.Size
            c = list1.Get(i)
            If c.DisplayName = name Then Exit
        Next
        If c.PHONE_MOBILE > 0 Then 'c.PhoneNumber.Length >
            edtNum.Text = c.PHONE_MOBILE
        Else
       
      edtNum.Text = "No mobile number"
      
      End If
            
    End If

End Sub
Regards
Bergapappa
 
Last edited by a moderator:

Erel

B4X founder
Staff member
Licensed User
Longtime User
c.PHONE_MOBILE is a constant value, with the value of 2.
You should do something like:
B4X:
   Dim phones As Map
   phones = c.GetPhones
   Dim mobilePhone As String
   For i = 0 To phones.Size - 1
      If phones.GetValueAt(i) = c.EMAIL_MOBILE Then
         mobilePhone = phones.GetKeyAt(i)
         Exit
      End If
   Next
   If mobilePhone <> "" Then
      EditText1.Text = mobilePhone
   End If
 
Upvote 0

chuckyegg

Member
Licensed User
Longtime User
It looks like your code could be quicker too, where it translates the selected contact back into a name for comparisson...

B4X:
    Dim res As Int
    res = InputList(listOfNames, "Choose contact", -1)
    If res <> DialogResponse.CANCEL Then
        Dim c As Contact
        c = listOfNames.Get(res)
        <!- Insert Erel's code here -!>
    End If


Bear in mind I might be wrong, if the InputList index is not the same as the listOfNames index. I don't even have B4A installed yet!
 
Upvote 0

bergapappa

Member
Licensed User
Longtime User
Thanks a lot guys.
I will try Chuckyeggs code after I get the first one correct.
Erel - first I think you made a misstake with c.EMAIL_MOBILE, should it not be PHONE_MOBILE

But I still get a error with the message "Contact object should be set by calling one of the Contacts methods"

My code now looks like this:

Dim Contacts2 As Contacts2
Dim list1 As List
list1 = Contacts2.GetAll(False,False)
Dim listOfNames As List
listOfNames.Initialize

For i = 0 To list1.Size - 1
Dim c As Contact
c = list1.Get(i)
If c.DisplayName.IndexOf("@") = -1 Then
listOfNames.Add(c.DisplayName)
End If
Next

Dim res As Int
res = InputList(listOfNames, "Choose contact", -1)

If res <> DialogResponse.CANCEL Then
Dim name As String
name = listOfNames.Get(Res)
Dim c As Contact

Dim phones As Map
phones = c.GetPhones
Dim mobilePhone As String
For i = 0 To phones.Size - 1
If phones.GetValueAt(i) = c.PHONE_MOBILE Then
mobilePhone = phones.GetKeyAt(i)
Exit
End If
Next
If mobilePhone <> "" Then
edtNum.Text = mobilePhone
Else
edtNum.Text = "Contact without phone!"
End If
 
Upvote 0

bergapappa

Member
Licensed User
Longtime User
Please

My logviewer say this line is the problem: phones = c.GetPhones

"Contact object should be set by calling one of the contacts methods"

I thought GetPhones was one of the Contacts methods?


Regards

bergapappa
 
Last edited:
Upvote 0

bergapappa

Member
Licensed User
Longtime User
I actually got that reading the code again i bed last night :)
Thx Erel, now I know what is missing and will give it a new try.
Good to get your confirmation on this so I'm not on a wild goose chase.

Regards
bergapappa
 
Upvote 0
Top