Android Question Help for threading

PoleStar

Member
Licensed User
Longtime User
Hello friends
Excuse me, I have a problem too with Threading !
No matter how I tried I could not work with Threading...

Suppose we want all your phone contacts are displayed in a list,
Now, if a large number of contacts we have app for quite some time hangs or mybe for closed...

For Example, if our code is:
B4X:
    For Each c As cuContact In cu.FindContactsByPhone("", False, False)
        For Each Phone As cuPhone In cu.GetPhones(c.Id)
            ListView1.AddSingleLine(c.DisplayName)
            ListView2.AddSingleLine(Phone.Number)
        Next
    Next

How to use the Thread in the code does not Hong programs?

Please help me :)
Thanks a lot
 
D

Deleted member 103

Guest
Not tested, but so should work.
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim TreadContact As Thread
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    Dim org(0) As Object
    TreadContact.Initialise("TreadContact")
    TreadContact.Start(Me,"Contact", org) 'Non passare "Null"
End Sub

Sub Contact(org As Object)
  For Each c As cuContact In cu.FindContactsByPhone("", False, False)
     For Each Phone As cuPhone In cu.GetPhones(c.Id)
       ListView1.AddSingleLine(c.DisplayName)
       ListView2.AddSingleLine(Phone.Number)
     Next
   Next
End Sub

Sub TreadContact_Ended(endedOK As Boolean, error As String) 'The thread has terminated. If endedOK is False error holds the reason for failure
    Log("error=" & error)
End Sub
 
Upvote 0

PoleStar

Member
Licensed User
Longtime User
Not tested, but so should work.
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim TreadContact As Thread
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    Dim org(0) As Object
    TreadContact.Initialise("TreadContact")
    TreadContact.Start(Me,"Contact", org) 'Non passare "Null"
End Sub

Sub Contact(org As Object)
  For Each c As cuContact In cu.FindContactsByPhone("", False, False)
     For Each Phone As cuPhone In cu.GetPhones(c.Id)
       ListView1.AddSingleLine(c.DisplayName)
       ListView2.AddSingleLine(Phone.Number)
     Next
   Next
End Sub

Sub TreadContact_Ended(endedOK As Boolean, error As String) 'The thread has terminated. If endedOK is False error holds the reason for failure
    Log("error=" & error)
End Sub

Thanks a lot
I tested your code.
show this error:
error= Exception : wrong number of arguments; expected 1,got 0


My Full Code Is:
B4X:
Sub Globals
    Private ListView1 As ListView
    Dim cu As ContactsUtils
    Private ListView2 As ListView
     Dim TreadContact As Thread
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout1")
    cu.Initialize
    Dim org(0) As Object
    TreadContact.Initialise("TreadContact")
    TreadContact.Start(Me,"Contact", org)
End Sub

Sub Contact(org As Object)
  For Each c As cuContact In cu.FindContactsByPhone("", False, False)
     For Each Phone As cuPhone In cu.GetPhones(c.Id)
       ListView1.AddSingleLine(c.DisplayName)
       ListView2.AddSingleLine(Phone.Number)
     Next
   Next
End Sub

Sub TreadContact_Ended(endedOK As Boolean, error As String)
    Msgbox("error=" & error,"")
End Sub
 
Upvote 0
D

Deleted member 103

Guest
Thanks a lot
I tested your code.
show this error:
error= Exception : wrong number of arguments; expected 1,got 0
Then make this change.
B4X:
Sub Contact
  For Each c As cuContact In cu.FindContactsByPhone("", False, False)
     For Each Phone As cuPhone In cu.GetPhones(c.Id)
       ListView1.AddSingleLine(c.DisplayName)
       ListView2.AddSingleLine(Phone.Number)
     Next
   Next
End Sub
 
Upvote 0

PoleStar

Member
Licensed User
Longtime User
Then make this change.
B4X:
Sub Contact
  For Each c As cuContact In cu.FindContactsByPhone("", False, False)
     For Each Phone As cuPhone In cu.GetPhones(c.Id)
       ListView1.AddSingleLine(c.DisplayName)
       ListView2.AddSingleLine(Phone.Number)
     Next
   Next
End Sub
Thanks for answer ♥
Show this error:
error= Exception : Only the original thread that created a view hierarchy can touch its views
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Dim cu As ContactsUtils
End Sub

Sub Globals
    Dim lstContacts As ListView
    Dim ImageView1 As ImageView
    Dim EditText1 As EditText
    Private lblPhonenum As Label
    Private lstPhonenums As ListView
    Dim TreadContacts As Thread

End Sub
Sub Contacts(org As Object) ' The thread
    Dim phonelist As List = org
    Dim Params(1) As Object   
    Log($"Contacs_started(${org})"$)
    'Log($"cu.FindContactsByPhone()"$)   
    For Each c As cuContact In cu.FindContactsByPhone("", False, False)
        Dim Map1 As Map
        Map1.Initialize
        Map1.Put("DisplayName",c.DisplayName)
        Map1.Put("Id",c.Id)
        'Log($"${c.DisplayName}"$)   
        'Log($"${c.Id}"$)   
        Dim phones As List
        phones.Initialize
        'Log($"cu.GetPhones(${c.Id})"$)   
      For Each phone As cuPhone In cu.GetPhones(c.Id)
            phones.Add(phone.Number)
            'Log($"Phone.Number(${phone.Number})"$)   
      'lstContacts.AddTwoLines2(c.DisplayName,Phone.Number, Phone)
    Next
        Map1.Put("Phones",phones)       
        phonelist.Add(Map1)
        Log($"Phonelist.Size ${phonelist.Size}"$)   
  Next
    Params(0) = phonelist
    ' The list is ready filled and is prepared to used by the next command which will send the reult to the mainthread
    TreadContacts.RunOnGuiThread("ContactsReady",Params)
    TreadContacts.Sleep(10)
End Sub
Sub Contacs_Ended(endedOK As Boolean, error As String) 'The thread has terminated. If endedOK is False error holds the reason for failure
    Log($"Contacs_Ended(${endedOK}, ${error})"$)
End Sub
Sub ContactsReady(contactslist As Object)
    Log($"ContactsReady()"$)   
    lstContacts.Clear
    Dim clist As List = contactslist
    For i=0 To clist.Size-1
        Dim contact As Map = clist.Get(i)
        lstContacts.AddTwoLines(contact.Get("DisplayName"),contact.Get("Id"))
        Dim phones As List = contact.Get("Phones")
        For o = 0 To phones.Size-1
            lstContacts.AddSingleLine(phones.Get(o))
        Next
        Log(clist.Get(i))       
    Next
End Sub
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        cu.Initialize
    End If
    Activity.LoadLayout("1")
  Dim MyList As List
  MyList.Initialize

    TreadContacts.Initialise("Contacts")
  TreadContacts.Start(Me,"Contacts", Array As Object(MyList))

   
    'For Each c As cuContact In cu.FindAllContacts(True)
        'lstContacts.AddSingleLine2(c.DisplayName, c)
    'Next
End Sub
 

Attachments

  • ContactsUtilsThreadListview.zip
    11.6 KB · Views: 107
Upvote 0
Top