Android Question Help for threading

Discussion in 'Android Questions' started by PoleStar, Apr 23, 2016.

  1. PoleStar

    PoleStar Member Licensed 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:
    Code:
    For Each c As cuContact In cu.FindContactsByPhone(""FalseFalse)
            
    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
     
  2. Filippo

    Filippo Expert Licensed User

  3. PoleStar

    PoleStar Member Licensed User

  4. Filippo

    Filippo Expert Licensed User

    Not tested, but so should work.
    Code:
    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(0As 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(""FalseFalse)
         
    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
     
    PoleStar likes this.
  5. PoleStar

    PoleStar Member Licensed User

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


    My Full Code Is:
    Code:
    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(0As Object
        TreadContact.Initialise(
    "TreadContact")
        TreadContact.Start(Me,
    "Contact", org)
    End Sub

    Sub Contact(org As Object)
      
    For Each c As cuContact In cu.FindContactsByPhone(""FalseFalse)
         
    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
     
  6. Filippo

    Filippo Expert Licensed User

    Then make this change.
    Code:
    Sub Contact
      
    For Each c As cuContact In cu.FindContactsByPhone(""FalseFalse)
         
    For Each Phone As cuPhone In cu.GetPhones(c.Id)
           ListView1.AddSingleLine(c.DisplayName)
           
    ListView2.AddSingleLine(Phone.Number)
         
    Next
       
    Next
    End Sub
     
    PoleStar likes this.
  7. PoleStar

    PoleStar Member Licensed User

    Thanks for answer ♥
    Show this error:
    error= Exception : Only the original thread that created a view hierarchy can touch its views
     
  8. DonManfred

    DonManfred Expert Licensed User

    Code:
    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(1As Object   
        
    Log($"Contacs_started(${org})"$)
        
    'Log($"cu.FindContactsByPhone()"$)   
        For Each c As cuContact In cu.FindContactsByPhone(""FalseFalse)
            
    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
     

    Attached Files:

    Filippo 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