Android Question Create X number of random contacts with miscUtil lib

Eric H

Active Member
Licensed User
Longtime User
I am trying to figure out how to use the miscUtil library to add "X" number of contacts, each with a randomly generated name and randomly generated phone number. The X value would be populated with a number entered into a EditText view and a button would activate the actual contact creation sequence.

The code example he uses is this:

B4X:
Sub Button1_Click
  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
  name = "Mike Tester"
  phoneNr = "004912345678"
  phoneType = c.PHONE_HOME
  mail = "[email protected]"
  mailType = c.EMAIL_HOME
  mu.createContactEntry(name, phoneNr, phoneType, mail, mailType) 
End Sub

How could I set this code into a loop to run X number of times and randomly generate the name="" and phoneNr="" fields?

Thanks for the help. I am new here, thanks for helping me get up to speed. :)
 

Eric H

Active Member
Licensed User
Longtime User
How far off am I here? It's been a long time since I have written any BASIC code. Any help and suggestions appreciated.


B4X:
Dim contactName() As String
Dim contactPhone() As Int
Dim quantity As Int


Sub Button1_Click

  For i = 1 to quantity
  
    contactPhone(i) = Rnd(1, 9999999999) 'phone number is random number with max 10 digits
    contactName(i) = ("#" & contactPhone) 'contact name is the same as phone number but has a # sign before it

    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
  
    name = contactName(i)
    phoneNr = contactPhone(i)
  
    phoneType = c.PHONE_HOME
    mail = ""
    mailType = c.EMAIL_HOME
    mu.createContactEntry(name, phoneNr, phoneType, mail, mailType)
  Next

End Sub
 
Last edited:
Upvote 0

Eric H

Active Member
Licensed User
Longtime User
I keep getting errors with this code:

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: libtest
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

'Activity module
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 contactName() As String
    Dim contactPhone() As Int
    Dim quantity As Int
    quantity = 10

End Sub

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

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    For i = 0 To (quantity -1)
       
       
        contactPhone(i) = Rnd(1000000000, 9999999999) 'phone number is random number with max 10 digits
        contactName(i) = ("#" & contactPhone(i)) 'contact name is the same as phone number but has a # sign before it
       
        '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 = contactName(i)
          phoneNr = contactPhone(i)
        phoneType = c.PHONE_HOME
        mail = ""
        mailType = c.EMAIL_HOME
         
        'create contact
        mu.createContactEntry(name, phoneNr, phoneType, mail, mailType)
       
      Next
         
End Sub


Here are the errors I get:

B4X:
** Activity (main) Create, isFirst = true **


** Activity (main) Resume **


** Activity (main) Pause, UserClosed = false **


** Activity (main) Create, isFirst = true **


** Activity (main) Resume **


main_button1_click (B4A line: 45)


contactPhone(i) = Rnd(1000000000, 9999999999) 'phone number is random number with max 10 digits
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0


    at lilivel.b4a.libtest.main._button1_click(main.java:312)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:63)
    at android.view.View.performClick(View.java:4204)
    at android.view.View$PerformClick.run(View.java:17355)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)


    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
 

Attachments

  • libtest.zip
    6.5 KB · Views: 260
Upvote 0
Top