Java Intent to Basic4Android

Pops

Member
Licensed User
Longtime User
I have been trying to convert the following java code to B4A code. The reason is that it shows all phone numbers of a contact. For instance, instead of just John, (number), it shows John, Home (number); John, Mobile (number); John, work (number):

B4X:
Intent intent = new Intent(Intent.ACTION_PICK, Phone.CONTENT_URI); 
        startActivityForResult(intent, PICK_CONTACT);
                  return false;

Thank for any help
 

Pops

Member
Licensed User
Longtime User
You cannot use StartActivityForResult directly. You will need to wrap it as a library. However this functionality is already supported by using Contacts2 (Phone library).

The following code is working as it should...except when a contact has multiple phone numbers it only allows the first one to be picked.
I think I'm not understanding the Contacts2 code well enough to implement that functionality. If you could help me out with the proper code I would very much appreciate it.
You're like a Chess Master, going from table to table making your moves, playing many games at the same time. I, for one, am impressed, and thankful for all the help you've been able to give.

B4X:
Sub Button1_Click
Dim list1 As List
   list1 = Contacts2.GetAll(True, False)
Dim listOfContacts As List
      listOfContacts.Initialize
For i = 0 To list1.Size - 1
Dim c As Contact
   c = list1.Get(i)
If c.DisplayName.IndexOf("@") = -1 Then
   listOfContacts.Add(c.DisplayName)
   listOfContacts.Sort(True)
End If
Next
   Dim res As Int
      res = InputList(listOfContacts, "Choose contact", -1)
   If res <> DialogResponse.CANCEL Then
   Dim name As String
      name = listOfContacts.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.Name.Length > 0 Then
      Label1.Text = name
   Dim m As Map
      m = c.GetPhones
   If m.Size > 0 Then
      Label2.Text = m.GetKeyAt(0)      
   Dim photo As Bitmap
      photo = c.GetPhoto
   If Photo <> Null Then
      Button1.SetBackgroundImage(photo)
   Else
      Button1.SetBackgroundImage(LoadBitmap(File.DirAssets,"NoPhoto.png"))
End If
End If
End If
End If
End Sub
 
Upvote 0

Pops

Member
Licensed User
Longtime User
Contact.GetPhones returns a map with the phone numbers as keys and their types as values.

The following code will print all the phone numbers:
B4X:
For i = 0 To m.Size - 1
      Log(m.GetKeyAt(i))
   Next

When this code runs:
B4X:
Dim res As Int
        res = InputList(listOfContacts, "Choose contact", -1)
It brings up a list of contacts to choose from. Some of those contacts have multiple phone numbers (Work, Home, Mobile). But the list doesn't give the option of selecting, for instance, the Work number, rather than Home or Mobile. I'm just not seeing how to do that.
I need to select the contact at this point so that the name and number can be placed on label1 and label2 and the photo, if it exists, on the button1.background.
 
Upvote 0

vb1992

Well-Known Member
Licensed User
Longtime User
try this

B4X:
Dim list1 As List
    list1 = Contacts2.GetAll(True, False)
Dim listOfContacts As List
        listOfContacts.Initialize
For i = 0 To list1.Size - 1
   
                     c = list1.Get(i)
                     
                  If c.DisplayName.IndexOf("@") = -1 Then
                     
                     
                     Dim m As Map
                          m = c.GetPhones
                            
                           If m.Size > 0 Then
                             
                           
                           
                           For CellHome = 0 To m.size-1
                           
                           If CellHome = 1 Then listOfContacts.Add(c.DisplayName & " (Home)")
                           If CellHome = 0 Then listOfContacts.Add(c.DisplayName & " (Cell)")
                           
                                 Log(c.DisplayName & ": " & m.GetKeyAt(CellHome))    
                           Next
                           
                           End If
                           
                           
                     listOfContacts.Sort(True)
                  End If
Next
 
Upvote 0
Top