Android Question Sorting a Contact list gives java.lang.CLassCastException

Discussion in 'Android Questions' started by Gerrard, Feb 25, 2015.

  1. Gerrard

    Gerrard Member Licensed User

    I have a program that lists the contacts in my phone in a listview. It works fine. Now I want to sort the listview. I have read similar posts and tried the suggestions but still get the ClassCastException error.
    This is the Sub that gives the error, in that last For loop at the line
    Contact = ListOfContacts.Get(i)

    Sub Getcontacts
    Dim Contacts2 As Contacts2
    Dim line As String
    Dim ListSize As Int
    Dim mylist As List
    mylist.Initialize
    ListOfContacts.Initialize
    ListOfContacts = Contacts2.GetAll(True, False)

    ListSize = ListOfContacts.Size - 1
    Log("Before mylist.add, ListSize=" & ListSize)
    For i = 0 To ListSize 'TESTING
    line = ListOfContacts.Get(i)
    mylist.add(line)
    Log("line " & i & line)
    Next

    mylist.Sort(True)

    For i = 0 To ListSize 'TESTING
    Log("Sort " & i & mylist.Get(i))
    Next

    ListOfContacts.Initialize
    For i = 0 To ListSize
    line = mylist.Get(i)
    ListOfContacts.Add(line)
    Log("Line "& i & line)
    Next

    For i = 0 To ListSize
    Dim Contact As Contact
    Contact = ListOfContacts.Get(i)
    Log("i=" & i & " " & Contact) 'TESTING
    If (Contact.PhoneNumber <>"" AND Contact.DisplayName <> "") Then
    lvphone.SingleLineLayout.Label.TextSize = 15
    lvphone.SingleLineLayout.ItemHeight = 40
    lvphone.AddSingleLine(Contact.DisplayName &": " &Contact.PhoneNumber)
    lvphone.ScrollingBackgroundColor =Colors.DarkGray
    End If
    Next
    End Sub

    What must I do to sort it out?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    1. Please use [code]code here...[/code] tags when posting code.
    2. You should post the full error message.

    I recommend you to use ContactUtils instead of Contacts2. With ContactUtils you will be able to easily sort the list with List.SortType.
     
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